我正在尝试实施一个用户可以向医生提交评论的评论表单。我在模板中有一个喜欢/不喜欢的按钮以及评论表单的其他字段。但是在视图中无法识别喜欢/不喜欢按钮。所以当我点击"提交评论"提交表单没有任何内容保存在数据库中。它甚至没有给我任何错误。
这是模板
<form action="" method="post" id="user_uploader" > {% csrf_token %}
<input type="radio" name="Like" value="Like">Like<br>
<input type="radio" name="Like" value="Dislike">Dislike
<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>How long did you have to wait?</b></option>
{% for value, text in form.time.field.choices %}
<option value="{{ value }}">{{ text }}</option>
{% endfor %}
</select>
<textarea type="text" class="form-control" id="comment" placeholder="Comment" name="comment" rows="8"></textarea>
<button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Submit Review</button>
</form>
</div>
以下是观点。我尝试使用print语句测试它是否在if语句中,但是没有任何内容被打印出来。
def addContent(request, id):
d = getVariables(request)
doctor = Doctor.objects.get(id=id)
if request.user.is_authenticated():
user = request.user
ds = DoctorSeeker.objects.get(user=user)
d['doctorseeker'] = ds
if request.method == "POST":
form = UserContentForm(request.POST)
if form.is_valid():
time = form.cleaned_data['time']
comment = form.cleaned_data['comment']
if request.POST.get('Like'):
con = UserContent(time=time, comment = comment, liked = True, disliked = False, doctor_id = doctor.id, user_id = request.user.id)
doctor.likes += 1
doctor.netlikes = doctor.likes - doctor.dislikes
con.save()
elif request.POST.get('Dislike'):
con = UserContent(time=time, comment = comment, liked = False, disliked = True, doctor_id = doctor.id, user_id = request.user.id)
doctor.dislikes +=1
doctor.netlikes = doctor.likes - doctor.dislikes
con.save()
url = '/docprofile/%s' % str(doctor.id)
return HttpResponseRedirect(url)
else:
form = UserContentForm()
d.update({'doctor': profile, 'UGC': UserContent.objects.all(),
'form': form })
return render(request, 'meddy1/usercontent.html',d)
答案 0 :(得分:1)
主要问题是喜欢和不喜欢的按钮不在表单内。只有表单元素中的元素才会提交给服务器。
然而,它并不清楚你期望按钮做什么。按钮元素不像复选框或单选按钮,您可以选择它们,然后与表单的其余部分一起提交:它们本身就是实际的提交按钮。