0

Please pardon me, am still new to djando framework. This error i always get i run it.

The view finance_lens_app.views.acct_type didn't return an HttpResponse object. It returned None instead. This is my model

class Acct_type(models.Model):
    Aid=models.AutoField(primary_key=True)
    acct_type_name=models.CharField(max_length=200)
    class Meta:
        db_table="Acct_Type"

This is my VIEW.

def acct_type(request):
    if request.method=='POST':
        request.POST.get('acct_type_name')
        acct_type_record=Acct_type()   
        acct_type_record.save()        
        messages.success(request, 'Account Type Added Successfully...')
        return render(request, "admin_opera/acct_type.html")  

My Interface HTML

<form method="POST"> 
  {% csrf_token %}
    <div class="input-group mb-3">
     <select name="acct_type_name" id="acct_type_name" class="form-control">
              <option value="Assets">Assets</option>
              <option value="Liabilities">Liabilities</option>
              <option value="Equities">Equities</option>
              <option value="Incomes">Incomes</option>
              <option value="Expenses">Expenses</option>
              <option value="Expenses2">Expenses2</option>
              <option value="Expenses3">Expenses3</option>
    </select>
   
   </div>
      <input class="btn btn-primary btn-lg" type="submit" name="submit" value="Add Account">
</form>

1 Answer 1

1

You need get the value of acct_type_name from the form, and then create the Acct_type instance and save.

def acct_type(request):
    if request.method=='POST':
        acct_type_name = request.POST.get('acct_type_name')
        acct_type_record=Acct_type(acct_type_name = acct_type_name)   
        acct_type_record.save()        
        messages.success(request, 'Account Type Added Successfully...')
        return render(request, "admin_opera/acct_type.html")  

If you want to check if a record exists, you need to implement the clean function through Django form or add unique=True to your model field.

class Acct_type(models.Model):
    Aid=models.AutoField(primary_key=True)
    acct_type_name=models.CharField(max_length=200, unique=True)
    class Meta:
        db_table="Acct_Type"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Gejun, i appreciate your support. it work for inserting (save) record but i want it to check if the record is existing before inserting the record.
Check this link. You can add unique=True to your model field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.