0
我試圖實現一個表單,我可以獲取userinput,但由於某種原因,表單不顯示在模板中。我在表格中有兩個字段,其中一個字段是下拉菜單。該模板未顯示下拉列表。Django 1.6:表單不顯示在模板中
這裏是我想要使用
TIME_CHOICES = (
(5, 'Less than 5 Minutes'),
(10, 'Less than 10 Minutes'),
(15, 'Less than 15 Minutes'),
)
class UserContentForm(forms.ModelForm):
time = forms.ChoiceField(required=True, choices = TIME_CHOICES, widget = forms.Select)
comment = forms.CharField(max_length=2000, required= False,widget=forms.TextInput())
class Meta:
model = UserContent
fields = ("time","comment")
這裏就是我tyring保存表單
def addContent(request, id):
d = getVariables(request)
profile = Doctor.objects.get(id=id)
if request.user.is_authenticated():
user = request.user
ds = DoctorSeeker.objects.get(user=user)
d['doctorseeker'] = ds
doctorLiked = Like.objects.filter(doctor_id=profile.id,user_id=user.id)
d['my_doctor'] = profile.id == request.user.id
d['username'] = user.username
if doctorLiked:
d['liked'] = True
else:
d['liked'] = False
if request.method == "POST":
form = UserContentForm(request.POST)
if form.is_valid():
time = form.cleaned_data['time']
comment = form.cleaned_data['comment']
con = UserContent(time=time, comment = comment, doctor_id = profile.id, user_id = request.user.id)
con.save()
return render(request,'meddy1/docprofile.html',{'doctor': profile})
else:
form = UserContentForm()
d.update({'doctor': profile, 'UGC': UserContent.objects.all()})
return render(request, 'meddy1/usercontent.html',d)
這裏的視圖的形式是,我的模板試圖使其
<form action="" method="post" id="user_uploader" > {% csrf_token %}
<input type="hidden" name="user" value="{{ user.id }}" />
<input type="hidden" name="doctor" value="{{ doctor.id }}" />
<select class="form-control" id="s1" name="time">
<option><b>Select a Time...</b></option>
{% for value, text in form.time.field.choices %}
<option value="{{ value }}">{{ text }}</option>
{% endfor %}
</select>
<input type="text" class="form-control" id="comment" placeholder="Comment" name="comment">
<button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Submit Review</button>
</form>
這裏是模型
class UserContent(models.Model):
time = models.IntegerField(blank = True)
comment = models.TextField(blank = True)
doctor = models.ForeignKey(Doctor)
user = models.ForeignKey(User)
submitted_on = models.DateTimeField(auto_now_add=True)
* Facepalm *非常感謝! –