'我是Django的新手,试图将表单数据保存在数据库中。创建了两个模型类PatientInfo
和patientHist
,它们继承了PatientInfo类。我不明白我要去哪里错了。
“。我没有收到任何错误,我的表是在数据库中创建的,但是当我单击提交按钮时,没有数据正在保存”
from django.db import models
# Create your models here.
class PatientInfo(models.Model):
sex = (
('M', 'Male'),
('F', 'Female')
)
first_name = models.CharField(max_length=35)
middle_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(max_length= 30)
sex = models.CharField(max_length=1,choices=sex)
date_of_birth = models.DateField()
height = models.FloatField()
weight = models.FloatField()
phone_no =models.CharField(max_length=15)
class PatientHist(PatientInfo):
Yes_No = (
(True, 'Yes'),
(False, 'No'),
)
Veg_nonveg =(
(True,'Veg'),
(False,'Non-Veg'),
)
diabetes = models.BooleanField(default=False,choices=Yes_No)
diabetes_long = models.CharField(max_length=20)
hypertension = models.BooleanField(default=False,choices=Yes_No)
hypertension_long = models.CharField(max_length=20)
obesity = models.BooleanField(default=False,choices=Yes_No)
obesity_long = models.CharField(max_length=20)
pcod = models.BooleanField(default=False,choices=Yes_No)
pcod_long= models.CharField(max_length=20)
thyroid = models.BooleanField(default=False,choices=Yes_No)
thyroid_long = models.CharField(max_length=20)
heartdiease = models.BooleanField(default=False,choices=Yes_No)
heartdiease_long = models.CharField(max_length=20)
liverdisease = models.BooleanField(default=False,choices=Yes_No)
liverdisease_long = models.CharField(max_length=20)
kidney = models.BooleanField(default=False,choices=Yes_No)
kidney_long = models.CharField(max_length=20)
familyhistory = models.BooleanField(default=False,choices=Yes_No)
currentmed = models.CharField(max_length=20)
foodhabit= models.BooleanField(default=False,choices= Veg_nonveg)
hba1c = models.FloatField(max_length=20)
fasting = models.FloatField(max_length=20)
pp = models.FloatField(max_length=20)
from django import forms from .models import *
class Patient_form(forms.ModelForm):
class Meta:
model = PatientInfo
fields = "__all__"
class PatientHistory_form(forms.ModelForm):
class Meta:
model = PatientHist
widgets = {
'diabetes': forms.RadioSelect,
'hypertension': forms.RadioSelect,
'obesity': forms.RadioSelect,
'pcod': forms.RadioSelect,
'thyroid': forms.RadioSelect,
'heartdiease': forms.RadioSelect,
'liverdisease': forms.RadioSelect,
'kidney':forms.RadioSelect,
'familyhistory' : forms.RadioSelect,
'currentmed':forms.RadioSelect,
'foodhabit':forms.RadioSelect,
}
fields = "__all__"
from django.shortcuts import render,redirect
from django.http import HttpResponse
from .forms import Patient_form,PatientHistory_form
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
# Create your views here.
#def home(request):
#return render(request,'home/base.html',{})
#def patient_view(request):
#context = {}
# context['form'] = Patient()
#return render(request, 'home/Patient_info.html', context)
#def patienthistory_view(request):
# context = {}
# context['history'] = PatientHistory_form
# return render(request, 'home/Patient_info.html', context)
def patienthistory_view(request):
if request.method == 'POST':
patientmedinfo = PatientHistory_form(request.POST)
if patientmedinfo.is_valid():
myid = patientmedinfo.save()
myid.save()
return HttpResponse( print(patientmedinfo.errors))
else:
patientmedinfo = PatientHistory_form()
return render(request, 'home/Patient_info.html', {'form': patientmedinfo})
{% extends "home/base.html" %}
{% block title %}Patient Information{% endblock title %}
{% block content %}
<form enctype="multipart/form-data" action=" " method="post" >
{% csrf_token %} <table align="center" border="0">
<tr>
<td><h4 align="center">Patient Information</h4></td>
<td>{{form}}</td>
<td><input align="center" type="submit" value=" Next--> "></td>
</tr> </table> </form>
{% endblock content %}
答案 0 :(得分:0)
嗨,终于我找到了你的错误。
您有一个currentmed
字段,CharField
。
但是在forms.py
中,您为其分配了forms.RadioSelect
小部件。因此,它为必填字段引发了错误。
因此,只需从'currentmed':forms.RadioSelect,
的{{1}}字典中删除widget
。
就是这样。