我正在试着为我的Q&;一个项目。我制作了模型以供评论,在question\u detail.html中的表单部分an,以及在form.py中的QuestionCommentForm()
model.py
class QuestionComment(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
question =
models.ForeignKey(Question,on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add= True)
body = models.CharField(max_length=200, null= True ,
blank = True, default ='')
def __str__(self):
return str(self.body)
forms.py
{%if c_form%}
<form method="POST" action= "{% usl 'blog:question-commet' question.id >{% csrf_token %}
{{c_form.media}}
{{ c_form.as_p}}
<button type = "submit" , name = "question_id", value = "{{question.pk}}", class ="btn btn-secondary btn-sm">submit comment</button>
views.py
@api_view(['POST','GET'])
def question_comment(request, *args, **kwargs):
form = QuestionCommentForm()
print('finction comment started'*20)
if request.method == 'POST':
c_form = QuestionCommentForm(request.POST)
if c_form.is_valid():
new_comment = c_form.save(commit=False)
new_comment.refresh_from_db()
c_form.instance.user = request.user
question = Question.objects.get(id = request.POST.get('question_id')
new_comment.question = question
new_comment.bldy =c_form.cleaned_data.get('body')
new_comment.save()
context['c_form'] = c_form
return render(request, 'blog/question_detail.html',context)
class QuestionDetail(DetailView):
template_name = 'blog/question_detail.html'
model = Question
context_object_name = 'question'
count_hit = True
def get_queryset(self):
return Question.objects.filter(id = self.kwargs['pk'])
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
c_form = QuestionCommentForm()
context = super(QuestionDetail,self).get_context_data(**kwargs)
self.obj= get_object_or_404(Question, id = self.kwargs['pk'])
self.object.save()
self.object.refresh_from_db()
answers = Answer.objects.filter (question_id = self.obj.id).order_by('-created_date')
liked =self.obj.like.filter(id =self.request.user.id).exists()
print('liked in class question not checked still' *10)
comments= QuestionComment.objects.filter(question = self.kwargs['pk'])
context['comments']= comments
context["answers"]=answers
context["liked "] = liked
context['c_form'] = c_form
return context
def post(request, *args, **kwargs):
print('post started'*100)
c_form = QuestionCommentForm()
c_form = QuestionCommentForm(request.POST)
if c_form.is_valid():
new_comment = c_form.save(commit=False)
new_comment.refresh_from_db()
new_comment.user = request.user
new_comment.question = c_form.cleaned_data.get('question_id')
new_comment.bldy =c_form.cleaned_data.get('body')
new_comment.save()
context['c_form'] = c_form
else:
c_form= QuestionCommentForm()
return render(request, 'blog/question_detail.html',context)
url.py
...
path('question-comment/<int:pk>/', question_comment, name = 'question-comment'),
]
在我看来,首先我尝试使用另一个函数来处理注释,但没有得到任何结果,并生成了一个def post() 在类QuestionDetial()中,表单仍然显示,但当我键入某个内容并按下按钮时,它刷新页面,而不保存任何内容。我已经用admin保存了一条评论,它出现在问题详细信息页面中。使用print查找bug,但类中的post()和问题\u comment()似乎没有被召回。搜索了很多,但没有答案。(顺便说一句,除了我修复的NoReverseMatch之外,我没有得到任何错误)
从不保存模型对象(不是通过窗体,也不是通过视图)。此外,该方法的名称是
'POST'
,而不是'Post'
:好的,问题是我在question\u detail模板中有两个表单标签,而在模板末尾添加评论表单的buy有3个,但问题是当我按下提交评论按钮时,终端显示了此消息
这是我为like函数制作的表单。(而我的url是问题注释) 我不小心忘记关闭模板中的like表单标记。所以我所需要的就是放一个
在窗体块之后。 谢谢,我是威廉·范昂森。它解决了我的post方法错误。 这些都很有帮助
Proper way to handle multiple forms on one page in Django
How can I build multiple submit buttons django form?
相关问题 更多 >
编程相关推荐