本文整理汇总了Python中forms.AnswerForm.setUser方法的典型用法代码示例。如果您正苦于以下问题:Python AnswerForm.setUser方法的具体用法?Python AnswerForm.setUser怎么用?Python AnswerForm.setUser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.AnswerForm
的用法示例。
在下文中一共展示了AnswerForm.setUser方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: question_details
# 需要导入模块: from forms import AnswerForm [as 别名]
# 或者: from forms.AnswerForm import setUser [as 别名]
def question_details(request, id):
try:
question = Question.objects.get(pk=id)
except Question.DoesNotExist:
raise Http404
if request.method == "POST":
user = request.user
if not user.is_authenticated():
return HttpResponseRedirect('/login/')
form = AnswerForm(request.POST)
form.setUser(user)
if form.is_valid():
answer = form.save()
question = answer.question
url = question.get_absolute_url()
return HttpResponseRedirect(url)
else:
default_data = {'question': question.id}
form = AnswerForm(initial=default_data);
answers = Answer.objects.filter(question=question).order_by('-added_at')
return render(request, 'question.html', {
'question': question,
'answers': answers,
'form': form,
})