DJANGO - The view location_form.views.Insertrecord didn't return an HttpResponse object. It returned None instead

I am having this error constantly - The view location_form.views.Insertrecord didn't return an HttpResponse object. It returned None instead.

I have tried indenting it in all ways possible. And i have a created another view in similar manner and that worked. Can someone tell where am i going wrong?

I want to store the data of this form in my database which is phpmyadmin (mysql)

views.py

from django.shortcuts import render
from .models import Location
from django.contrib import messages

# Create your views here.

def Insertrecord(request):
    if request.method=='POST':
        if request.POST.get('id') and request.POST.get('parent_id') and request.POST.get('name') and request.POST.get('status') and request.POST.get('added_by') and request.POST.get('updated_by') and request.POST.get('created_on') and request.POST.get('updated_on') :
            saverecord=Location()
            saverecord.id=request.POST.get('id')
            saverecord.parent_id=request.POST.get('parent_id')
            saverecord.name=request.POST.get('name')
            saverecord.status=request.POST.get('status')
            saverecord.added_by=request.POST.get('added_by')
            saverecord.updated_by=request.POST.get('updated_by')
            saverecord.created_on=request.POST.get('created_on')
            saverecord.updated_on=request.POST.get('updated_on')
            saverecord.save()
            messages.success(request,"Record Saved Successfully..!")
            return render(request, 'location.html')
    else:
        return render(request,'location.html')

location.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Location Registration Form</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    <nav class="navbar bg-light">
        <div class="container-fluid">
          <a class="navbar-brand" href="#">
            <img src="https://img.freepik.com/premium-vector/customer-service-icon-vector-full-customer-care-service-hand-with-persons-vector-illustration_399089-2810.jpg?w=2000" alt="" width="30" height="24" class="d-inline-block align-text-top">
            Location Registration Portal
          </a>
        </div>
      </nav>
</head>

  <body background="https://img.freepik.com/free-vector/hand-painted-watercolor-pastel-sky-background_23-2148902771.jpg?w=2000">
    <br>
    <h1 align="center">Location Registration Portal</h1>
    <hr>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
    <div class="container-sm">
      <form method = "POST">
        {% csrf_token %}

        <div class="row mb-3">
          <label for="inputEmail3" class="col-sm-2 col-form-label">Country</label>
          <div class="col-sm-10">
            <input type="text" name="name" class="form-control" id="inputEmail3"/>
          </div>
        </div>

        <div class="row mb-3">
            <label for="inputEmail3" class="col-sm-2 col-form-label">Status</label>
            <div class="col-sm-10">
            <select class="form-select" aria-label="Default select example">
            <option selected>--Select Status--</option>
            <option value="1">Active</option>
            <option value="0">Inactive</option>
          </select>
        
          <br><br>
        {% if messages %}
    {% for message in messages %}
        <h2 style="color: green;">{{message}}</h2>
    {% endfor %}
    {% endif %}

        <div class="d-grid gap-2 col-6 mx-auto">
            <input style="color: black;" type="submit" value="Submit"/>
        </div>
        <br>
      </form></div>
  </body>
</html>

urls. py

from django.contrib import admin
from django.urls import path
from customer_form import views as cview
from location_form import views as lview

urlpatterns = [
    path('admin/', admin.site.urls),
    # path('', admin.site.login, name='login'), 
    path('', cview.Insertrecord),
    path('location/', lview.Insertrecord),
]

It is giving none because you are rendering template inside if statement.

def Insertrecord(request):
    if request.method=='POST':
        if request.POST.get('id') and request.POST.get('parent_id') and request.POST.get('name') and request.POST.get('status') and request.POST.get('added_by') and request.POST.get('updated_by') and request.POST.get('created_on') and request.POST.get('updated_on') :
            saverecord=Location()
            saverecord.id=request.POST.get('id')
            saverecord.parent_id=request.POST.get('parent_id')
            saverecord.name=request.POST.get('name')
            saverecord.status=request.POST.get('status')
            saverecord.added_by=request.POST.get('added_by')
            saverecord.updated_by=request.POST.get('updated_by')
            saverecord.created_on=request.POST.get('created_on')
            saverecord.updated_on=request.POST.get('updated_on')
            saverecord.save()
            messages.success(request,"Record Saved Successfully..!")
            return redirect('/home') #Add your route name here
    return render(request, 'location.html') 
    
Back to Top