我正在尝试创建一个可以为人们创建约会的表单。它将通过从数据库中获取数据来填充选择框。
models.py:
class customer(models.Model):
# Need autoincrement, unique and primary
cstid = models.AutoField(primary_key=True, unique=True, default=1)
name = models.CharField(max_length=35)
age=models.IntegerField()
mobile = models.CharField(max_length=15)
place = models.CharField(max_length=80)
def __str__(self):
return self.name
class doctor(models.Model):
docid = models.AutoField(primary_key=True, unique=True) # Need autoincrement, unique and primary
name = models.CharField(max_length=35)
regid = models.CharField(max_length=15, default="", blank=True)
photo = models.CharField(
max_length=35, default="", blank=True)
email = models.EmailField(default="", blank=True)
phone = models.CharField(max_length=15)
qualifications = models.CharField(
max_length=50, default="", blank=True)
about = models.CharField(
max_length=35, default="", blank=True)
specialities = models.CharField(
max_length=50, default="", blank=True)
department = models.CharField(max_length=50, default="ENT", blank=True)
fees = models.FloatField(default=300.0)
displayfee = models.IntegerField(default=0, blank=True)
slotrange = models.CharField(max_length=50, blank=True)
slotdurn = models.IntegerField(default=10)
breakrange = models.CharField(
max_length=50, default="", blank=True)
slotsleft = models.CharField(
max_length=50, default="", blank=True)
def __str__(self):
return self.name
class appointment(models.Model):
# Need autoincrement, unique and primary
appid = models.AutoField(primary_key=True, unique=True, default=1)
date = models.DateField(default=timezone.now)
time = models.TimeField(default=timezone.now)
docid = models.ForeignKey(doctor, on_delete=models.CASCADE, null=True)
cstid = models.ForeignKey(customer, on_delete=models.CASCADE, null=True)
表单-forms.py:
class AddAppointmentForm(forms.Form):
docchoices = []
for doc in doctor:
docst = [doc.docid, doc.name]
docchoices.append(docst)
name = forms.CharField(label='Name', max_length=100)
gender = forms.ChoiceField(
required=True,
widget=forms.RadioSelect,
choices=docchoices,
)
age = forms.IntegerField(max_value=100,min_value=1, required=True)
phone = forms.CharField(label='Phone', max_length=14)
email = forms.CharField(label='Email', max_length=25)
address = forms.Textarea()
city = forms.CharField(label='Email', max_length=20)
doctors = forms.ChoiceField(
required=True,
widget=forms.Select,
choices=docchoices,
)
views.py的相关部分:
def get_createappointmentform(request):
if request.method == 'POST':
form = AddAppointmentForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('appointments/addapp.html')
else:
form = AddAppointmentForm()
return render(request, 'appointments/name.html', {'form': form})
模板-name.html
<form action="/addapp.html" method="post">
{% csrf_token %} {{ form }}
<input type="submit" value="Submit" />
</form>
当我尝试加载页面http://127.0.0.1:8000/appointments/getappointment时,它不会加载。命令行显示如下:
File "/home/joel/myappointments/appointments/urls.py", line 3, in <module>
from . import views
File "/home/joel/myappointments/appointments/views.py", line 6, in <module>
from .forms import NameForm
File "/home/joel/myappointments/appointments/forms.py", line 7, in <module>
class AddAppointmentForm(forms.Form):
File "/home/joel/myappointments/appointments/forms.py", line 9, in AddAppointmentForm
for doc in doctor:
TypeError: 'ModelBase' object is not iterable
我在做什么错了?