Django:Ошибка при попытке добавить данные в базу данных postgres
У меня возникает ошибка (Представление crud.views.insertEmp не вернуло объект HttpResponse. Вместо этого он вернул None.) всякий раз, когда я пытаюсь вставить данные в свою базу данных через шаблон insert.html. В качестве базы данных я использую postgres. Любая помощь в выявлении ошибки будет высоко оценена.
models.py
from django.db import models
class Employee(models.Model):
emp_name = models.CharField(max_length=100)
email = models.CharField(max_length=100)
occupation = models.CharField(max_length=100)
salary = models.IntegerField()
gender = models.CharField(max_length=1)
class Meta:
db_table = 'employee'
views.py
from django.shortcuts import render, redirect
from crud.models import Employee
from django.contrib import messages
def showEmp(request):
employee = Employee.objects.all()
context = {"data": employee}
return render(request, "index.html", context)
def insertEmp(request):
if request.method == "POST":
if request.POST.get('emp_name') and request.POST.get('email') and request.POST.get('occupation') and request.POST.get('salary') and request.POST.get('gender'):
saveRecord = Employee()
saveRecord.emp_name = request.POST.get('emp_name')
saveRecord.email = request.POST.get('email')
saveRecord.occupation = request.POST.get('occupation')
saveRecord.salary = request.POST.get('salary')
saveRecord.gender = request.POST.get('gender')
saveRecord.save()
messages.success(request, "Employee " +
saveRecord.emp_name + " is added succesfully.")
return render(request, "insert.html")
else:
return render(request, "insert.html")
inser.html
<center>
<h1>Insert data into Postgresql</h1>
<hr>
<form method="POST">
{% csrf_token %}
<table border="">
<tr>
<td>Employee Name</td>
<td><input type="text" placeholder=" Enter employee name.." name="emp_name""></td>
</tr>
<tr>
<td>Email</td>
<td><input type=" text" placeholder="Enter email" name="email">
</td>
</tr>
<tr>
<td>Occupaton</td>
<td>
<select name="occupation">
<option selected disabled=true>-- Select occupation --</option>
<option value="">Programmer</option>
<option value="">HR</option>
<option value="">Sales Manager</option>
<option value="">Designer</option>
</select>
</td>
</tr>
<tr>
<td>Salary</td>
<td><input type="text" placeholder="Enter salary" name="salary"></td>
</tr>
<tr>
<td>Gender</td>
<td>
<input type="radio" name="gender" value="M">Male |
<input type="radio" name="gender" value="F">Female
</td>
</tr>
<tr>
<td><input type="submit" value="Insert"></td>
<td>
{% if messages %}
{% for message in messages %}
<b style="color:green;">{{message}}</b>
{% endfor %}
{% endif %}
</td>
</tr>
</table>
</form>
</center>
То, как вы структурируете свой код, может привести к возврату None, попробуйте следовать этому примеру :
from django.shortcuts import render, redirect
def insertEmp(request):
if request.method == "POST":
if request.POST.get('emp_name') and request.POST.get('email') and request.POST.get('occupation') and request.POST.get('salary') and request.POST.get('gender'):
saveRecord = Employee()
saveRecord.emp_name = request.POST.get('emp_name')
saveRecord.email = request.POST.get('email')
saveRecord.occupation = request.POST.get('occupation')
saveRecord.salary = request.POST.get('salary')
saveRecord.gender = request.POST.get('gender')
saveRecord.save()
messages.success(request, "Employee " +
saveRecord.emp_name + " is added succesfully.")
# Redirect to the same page (you can change the page to fit your needs)
return redirect('.')
else:
# Not all required fields are present
messages.error(request, "Please fill all the fields")
# Also redirect the user to the same page
return redirect('.')
else:
return render(request, "insert.html")