Reputation: 1153
I'm trying to implement a form where I can get userinput, but for some reason the form in not showing in the template. I have two fields in the form and one of the field is a dropdown menu. The template is not showing the dropdown list.
Here is the form that I'm trying to use
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")
Here is the view where I'm tyring to save the form
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)
here is the template where I'm trying to render it
<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>
Here is the model
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)
Upvotes: 0
Views: 57
Reputation: 53326
You are not passing the form
variable to template. Update the line to
d.update({'doctor': profile, 'UGC': UserContent.objects.all(),
'form': form #add form variable
})
Also, instead of manually rendering select tag you can do {{ form.time }}
to render it.
Upvotes: 2