Я хочу отправить свои данные через почтовый запрос. Я использовал базу данных postgres.
Я также пытался выполнить makemigrations и перенести эту модель, также отображается сообщение об ошибке.
Когда я пытался отправить свою форму, она показывает следующую ошибку.
IntegrityError at /formSubmit
null value in column "id" violates not-null constraint
DETAIL: Failing row contains (Own, Khar, 3, 23, 2323, 23233, 324, null).
Request Method: POST
Request URL: http://127.0.0.1:8000/formSubmit
Django Version: 2.1.7
Exception Type: IntegrityError
Exception Value:
null value in column "id" violates not-null constraint
DETAIL: Failing row contains (Own, Khar, 3, 23, 2323, 23233, 324, null).
Exception Location: D:\coding time\Django+ GeoDjango\Django
Enviroment\venv\lib\site-packages\django\db\backends\utils.py in _execute,
line 85
Python Executable: D:\coding time\Django+ GeoDjango\Django
Enviroment\venv\Scripts\python.exe
Python Version: 3.7.2
мой файл models.py поставляется из команды python manage.py inspectdb. Я управлял базой данных в postgres. Файл models.py находится здесь:
class Form(models.Model):
name = models.CharField(max_length=30, blank=True, null=True)
email = models.CharField(max_length=29, blank=True, null=True)
password = models.CharField(max_length=30, blank=True, null=True)
class Meta:
db_table = 'form'
class Ownership(models.Model):
house_no = models.IntegerField(blank=True, null=True)
ward_no = models.IntegerField(blank=True, null=True)
tole = models.CharField(max_length=100, blank=True, null=True)
house_owner = models.CharField(max_length=100, blank=True, null=True)
gender = models.CharField(max_length=100, blank=True, null=True)
religion = models.CharField(max_length=100, blank=True, null=True)
language = models.CharField(max_length=100, blank=True, null=True)
class Meta:
db_table = 'ownership'
class Residence(models.Model):
ownership_type = models.CharField(max_length=100, primary_key=True)
house_type = models.CharField(max_length=100, blank=True, null=True)
land_type = models.CharField(max_length=100, blank=True, null=True)
total_room = models.CharField(max_length=101, blank=True, null=True)
house_use = models.CharField(max_length=101, blank=True, null=True)
house_area = models.CharField(max_length=101, blank=True, null=True)
earthquake_resistance = models.CharField(max_length=101, blank=True, null=True)
id = models.ForeignKey(Ownership, models.DO_NOTHING, db_column='id')
class Meta:
db_table = 'residence'
class ServiceDetail(models.Model):
radio = models.BooleanField(primary_key=True)
television = models.BooleanField(blank=True, null=True)
telephone = models.BooleanField(blank=True, null=True)
computer = models.BooleanField(blank=True, null=True)
internet = models.BooleanField(blank=True, null=True)
motorcycle = models.BooleanField(blank=True, null=True)
car = models.BooleanField(blank=True, null=True)
refrigerator = models.BooleanField(blank=True, null=True)
washing_machine = models.BooleanField(blank=True, null=True)
heater = models.BooleanField(blank=True, null=True)
id = models.ForeignKey(Ownership, models.DO_NOTHING, db_column='id')
class Meta:
db_table = 'service_detail'
мой файл form.html находится здесь:
<html>
<head>
<title>form</title>
<style type="text/css">
table {
width:100%;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
text-align: left;
}
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color: #fff;
}
table#t01 th {
background-color: black;
color: white;
}
</style>
</head>
<body>
<form method="POST" action='/formSubmit'>
{% csrf_token %}
<h3>Ownership Detail</h3>
Name: <input type="text" name="name"><br><br>
Ward: <input type="text" name="ward"><br><br>
tole name: <input type="text" name="tname"><br><br>
House number: <input type="text" name="hnumber"><br><br>
Gender: <input type="radio" name="gender" value="male">Male <input type="radio" name="gender" value="male">Female<br><br>
Religeous: <select name="religeous">
<option>Select</option>
<option>Hindu</option>
<option>Buddhist</option>
<option>Islam</option>
<option>Isai</option>
</select>
<br><br>
language: <input type="text" name="language"> <br><br>
<h3>Residence Detail</h3>
Owner type: <select name="ownertype"><option>select</option><option>Own</option><option>Lease</option><option>Corporation</option></select><br><br>
House type: <select name="housetype">
<option>select</option>
<option>Khar</option>
<option>Tin</option>
<option>Cement</option>
<option>Stone</option>
</select><br><br>
Land type: <input type="text" name="landtype"><br><br>
Total room: <input type="text" name="totalroom"><br><br>
House used: <input type="text" name="houseused"><br><br>
House area: <input type="text" name="housearea"><br><br>
Earthquake resistance: <input type="text" name="earthquake"><br><br>
<h3>Service Detail</h3>
<table>
<tr>
<th>Facilities</th>
<th>Yes/no</th>
</tr>
<tr>
<td>Radio</td>
<td><select name="radio"><option>Yes</option><option>No</option></select></td>
</tr>
<tr>
<td>TV</td>
<td><select name="tv"><option>Yes</option><option>No</option></select></td>
</tr>
<tr>
<td>Mobile</td>
<td><select name="mobile"><option>Yes</option><option>No</option></select></td>
</tr>
<tr>
<td>computer</td>
<td><select name="computer"><option>Yes</option><option>No</option></select></td>
</tr>
<tr>
<td>Internet</td>
<td><select name="internet"><option>Yes</option><option>No</option></select></td>
</tr>
<tr>
<td>MoterCycle</td>
<td><select name="motercycle"><option>Yes</option><option>No</option></select></td>
</tr>
<tr>
<td>Car</td>
<td><select name="car"><option>Yes</option><option>No</option></select></td>
</tr><tr>
<td>Hitar</td>
<td><select name="hitar"><option>Yes</option><option>No</option></select></td>
</tr>
</table>
<br><br>
<p align="center"><button type="submit" name="submit">Submit Data</button></p>
</form>
</body>
</html>
мой файл views.py находится здесь:
from .models import Form, Residence, Ownership, ServiceDetail
# Create your views here.
def index(request):
form = Form.objects.all()
context = {
'form': form
}
return render(request, 'index.html', context)
def form(request):
return render(request, 'form.html')
def submit(request):
name = request.POST['name']
email = request.POST['email']
password = request.POST['password']
info = Form(name=name, email=email, password=password)
info.save()
return render(request, 'submit.html')
def formSubmit(request):
name = request.POST['name']
ward = request.POST['ward']
tname = request.POST['tname']
hnumber = request.POST['hnumber']
gender = request.POST['gender']
religeous = request.POST['religeous']
language = request.POST['language']
ownertype = request.POST['ownertype']
housetype = request.POST['housetype']
landtype = request.POST['landtype']
totalroom = request.POST['totalroom']
houseused = request.POST['houseused']
housearea = request.POST['housearea']
earthquake = request.POST['earthquake']
radio = request.POST['radio']
tv = request.POST['tv']
computer = request.POST['computer']
mobile = request.POST['mobile']
internet = request.POST['internet']
motercycle = request.POST['motercycle']
car = request.POST['car']
hitar = request.POST['hitar']
def count(n):
for i in range(n):
return i
if i == n:
return n+1
ownership = Ownership(house_no=hnumber, ward_no=ward, tole=tname, house_owner=name, gender=gender, religion=religeous, language=language)
ownership.save()
residence = Residence(ownership_type=ownertype, house_type=housetype, land_type=landtype, total_room=totalroom, house_use=houseused, house_area=housearea, earthquake_resistance=earthquake)
residence.save()
serviceDetail = ServiceDetail(radio=radio, television=tv, computer=computer, internet= internet, motorcycle=motercycle, car=car, heater=hitar)
serviceDetail.save()
return render(request, 'submit.html')
мой файл urls.py находится здесь:
from . import views
urlpatterns = [
path('', views.index, name = 'index'),
path('form', views.form, name = 'form'),
path('submit', views.submit, name = 'submit'),
path('formSubmit', views.formSubmit, name= 'formSubmit')
]