Home > OS >  The view Insertemp.views.Insertrecord didn't return an HttpResponse object. It returned None in
The view Insertemp.views.Insertrecord didn't return an HttpResponse object. It returned None in

Time:01-13

I'm not sure what is coursing the problem....

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

views.py

from django.shortcuts import render
from Insertemp.models import EmpInsert
from django.contrib import messages

def Insertrecord(request):
    if request.method == 'POST':
        if request.POST.get('empname') and request.POST.get('email') and request.POST.get('country'):
            saverecord = EmpInsert()
            saverecord.empname = request.POST.get('empname')
            saverecord.email = request.POST.get('email')
            saverecord.country = request.POST.get('country')
            saverecord.save()
            messages.success(request, 'Record saved successfully...!')
            return render(request, 'Index.html')
        else:
            return render(request, 'Index.html'),

I have change the code....

from django.shortcuts import render
from Insertemp.models import EmpInsert
from django.contrib import messages

def Insertrecord(request):
    if request.method == 'POST':
        if request.POST.get('empname') and request.POST.get('email') and request.POST.get('country'):
            saverecord = EmpInsert()
            saverecord.empname = request.POST.get('empname')
            saverecord.email = request.POST.get('email')
            saverecord.country = request.POST.get('country')
            saverecord.save()
            messages.success(request, 'Record saved successfully...!')
            return render(request, 'templates/Index.html', {})
        else:
            return render(request, 'templates/Index.html', {})

but still it doesn't display what it should...

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Insert new Record</title>
</head>
<body>
    <h1>Create or New record Insert into MySQL(PhpMyAdmin)</h1>
    <h2>Python Django web Programming</h2>
    <hr>
    <form method="POST">
        {% cdrf_token %}
        Employee Name : <input type="text" placeholder="Enter Name" name="empname" required>
        Email : <input type="text" placeholder="Enter Email" name="email" required>
        country : <input type="text" placeholder="Enter Country" name="country" required>
        <input type="submit" value="Insert record">

        {% if messages %}
            {% for message in messages %}
                <h2 style="color: green;">{{message}}</h2>
            {% endfor %}
        {% endif %}
    </form>
    <hr>
</body>
</html>

CodePudding user response:

You need to give default response, in example for GET requests:

def Insertrecord(request):
    if request.method == 'POST':
        if request.POST.get('empname') and request.POST.get('email') and request.POST.get('country'):
            (...)
            return render(request, 'templates/Index.html', {})
        else:
            return render(request, 'templates/Index.html', {})
    return render(request, 'templates/Index.html', {})  # new
  • Related