[英]Add Comment In Django
I have code but when I write comment not write it , and error "POST /post_list HTTP/1.1" 405 0 我有代码,但是写注释时不写,并且错误“ POST / post_list HTTP / 1.1” 405 0
in views.py : 在views.py中:
class AddPost(View):
def get(self,request):
form = AddPostForm()
context = {'form':form}
return render(request,'add_post.html',context)
def post(self,request):
form = AddPostForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
else:
context = {'form':form}
return render(request,'add_post.html',context)
in forms.py 在forms.py中
class AddCommentForm(ModelForm):
def __init__(self,*args,**kwargs):
super(AddCommentForm,self).__init__(*args,**kwargs)
self.helper = FormHelper(self)
self.helper.layout.append(Submit('submit','Add'))
class Meta:
model = Comment
exclude = ['post']
labels = {
'name' : 'Nickname',
'comment' : '',
}
error_messages = {
'name':{
'unique' : 'This Title has been used !'
}
}
in post_content.html 在post_content.html中
{% extends 'base.html' %}
{% load static %}
{% block title %} {{ post.title }} {% endblock %}
{% block content %}
<div align="center" class="divtw" style="width: 50%">
<h1>{{ post.title }}</h1>
<h2>{{ post.text }}</h2>
<h5>{{ post.author }}</h5>
<hr>
{% for comments in post.comment_set.all %}
<div align="left">
<h5>{{ comments.name }}</h5>
<p>{{ comments.comment }}</p>
</div>
<hr>
{% endfor %}
</div>
<div align="left">
{% include 'add_comment.html' %}
</div>
{% endblock %}
in add_comment.html 在add_comment.html中
{% load crispy_forms_tags %}
<form method="post" action="/post_list">
{% csrf_token %}
{% crispy form %}
</form>
when I add comment by admin page it comes with out error for more info this is link for download tutorial and if may tell me what's the problem and how can I save it 当我通过管理页面添加评论时,它带有错误的详细信息,这是下载教程的链接 ,如果可以告诉我是什么问题以及如何保存它
The action of your comment form is pointing to "/post_list", which presumably is a listview that renders the list of posts. 您的评论表单的操作指向“ / post_list”,它大概是一个呈现帖子列表的列表视图。 That view doesn't know what to do with the POST request, hence the 405 error. 该视图不知道如何处理POST请求,因此出现405错误。
It should be pointing to the URL of a view that processes and saves the comment. 它应该指向处理并保存注释的视图的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.