【问题标题】:Errors using built-in comment framework使用内置评论框架的错误
【发布时间】:2011-04-14 01:25:00
【问题描述】:

我正在尝试使用内置的评论框架,但我无法让它工作。代码如下:

#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)

任何帮助将不胜感激,干杯。

【问题讨论】:

  • 虽然不是解决方案的一部分,但您应该谨慎使用if request.POST:,而不是使用if request.method == 'POST':

标签: django django-forms django-comments


【解决方案1】:

很抱歉,如果您使用内置的评论框架,在阅读您的 cmets 到我的最后一个答案后,您不需要在视图中包含评论表单:

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 %}

您希望 cmets 显示的位置:

{% render_comment_list for item %}

您希望添加 cmets 表单显示在哪里:

{% 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 %}

【讨论】:

  • 对不起,我没有澄清这一点,但我想要有人发表关于某个项目的评论并让评论出现在同一页面上。 (item_detail.html)。在上面的答案中, (r'^cmets/', include('django.contrib.cmets.urls')) 如果我是对的并且我不想这样做,则会将用户发送到不同的页面。我想使用他们的框架,但我希望所有内容都重定向到同一个网页。除了创建自定义评论类之外,还有其他方法吗?
  • 谢谢,工作就像一个魅力。只需要摆脱预览页面就可以了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
相关资源
最近更新 更多