使用内置注释框架时出错

| 我正在尝试使用内置的注释框架,但无法使其正常工作。这是代码:
#view.py
from django.contrib.comments.forms import *
from forms import *
from models import *

def view_item_detail(request, item_id):
    item = Item.manager.get(item_id)
    form = CommentForm(item)

    if request.POST:
        form = CommentForm(request.POST)
        if form.is_valid():
            new_comment = form.save(commit=False)
            # do stuff here
            new_comment.save()
            messages.success(request, \"Your comment was successfully posted!\")
            return HttpResponseRedirect(\"\")

    return render_to_response(\'item_detail.html\', 
                          RequestContext(request, {\'item\': item, 
                                    \'authentication\': request.user.is_authenticated(), 
                                    \'user\': request.user, \'form\': form}))
#item_detail.html
{% if authentication %}
    {% if form %}
        <form action=\"\" method=\"post\">{% csrf_token %}
            {{ form }}
            <p><input type=\"submit\" name=\"submit\" value=\"Submit comment\" /></p>
        </form>
    {% endif %}
{% else %}
    <p>You must be logged-in to post a comment</p>
{% endif %}
我收到的错误是\“ \'QueryDict \'对象没有属性''_meta \'\',该属性来自行
form = CommentForm(request.POST)
任何帮助将不胜感激,欢呼。     
已邀请:
抱歉,阅读完我对最后一个答案的评论后,如果您使用内置的评论框架,则无需在视图中包括评论表单:
from forms import *
from models import *

def view_item_detail(request, item_id):
    item = get_object_or_404(Item, pk=item_id)

    return render_to_response(\'item_detail.html\', 
                          RequestContext(request, {\'item\': item, 
                                    \'authentication\': request.user.is_authenticated(), 
                                    \'user\': request.user,}))
现在确保您的urls.py中包含以下内容:
urlpatterns = patterns(\'\',
    ...
    (r\'^comments/\', include(\'django.contrib.comments.urls\')),
    ...
)
\'django.contrib.comments\'
已添加到您的INSTALLED_APPS中,并且已同步 现在,在您的
item_detail.html
文件中,您应该添加:
{% load comments %}
您要在其中显示评论的位置:
{% render_comment_list for item %}
您要在其中显示添加评论表单的位置:
{% if authentication %}
    {% get_comment_form for item as form %}
    <form action=\"{% comment_form_target %}\" method=\"post\">
        {{ form }}
        <tr>
            <td></td>
            <td><input type=\"submit\" name=\"preview\" class=\"submit-post\" value=\"Preview\"></td>
        </tr>
    </form>
{% endif %}
请阅读文档
here
,要进行自定义,请阅读
this
页。 作为文档的一部分:   指定您想要的URL   重定向到评论后   发布,您可以包含一个隐藏的表单   您评论中的下一个输入   形成。例如:
<input type=\"hidden\" name=\"next\" value=\"{% url my_comment_was_posted %}\" />
(为您的示例编辑):
{% if authentication %}
    {% get_comment_form for item as form %}
    <form action=\"{% comment_form_target %}\" method=\"post\">
        {{ form }}
        <tr>
            <td></td>
            <input type=\"hidden\" name=\"next\" value=\"{{ item.get_absolute_url }}\" />
            <td><input type=\"submit\" name=\"preview\" class=\"submit-post\" value=\"Preview\"></td>
        </tr>
    </form>
{% else %}
    <p>You must be logged-in to post a comment</p>
{% endif %}
    

要回复问题请先登录注册