1

組み込みのコメントフレームワークを使用しようとしていますが、機能させることができません。コードは次のとおりです。

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

助けていただければ幸いです、乾杯。

4

1 に答える 1

1

私の最後の回答に対するコメントを読んだ後、組み込みのコメントフレームワークを使用している場合は、ビューにコメントフォームを含める必要はありません。

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に追加され、syncdbされました

今あなたの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を指定するには、コメントフォームにnextという非表示のフォーム入力を含めることができます。例えば:

<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 %}
于 2011-04-14T02:18:24.290 に答える