Search code examples
djangodjango-formsdjango-comments

Errors using built-in comment framework


I'm trying to use the built-in comment framework but I cannot get it to work. Here's the code:

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

and

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

The error I'm getting is "'QueryDict' object has no attribute '_meta'" which is coming from the line

form = CommentForm(request.POST)

Any help would be appreciated, cheers.


Solution

  • Sorry after reading your comments to my last answer you don't need to include the comment form in your view if you are using the built in comment framework:

    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,}))
    

    now make sure you have this in your urls.py:

    urlpatterns = patterns('',
        ...
        (r'^comments/', include('django.contrib.comments.urls')),
        ...
    )
    

    and 'django.contrib.comments' added to your INSTALLED_APPS, and syncdb'd

    now in your item_detail.html file you should add:

    {% load comments %}
    

    where you want the comments displayed:

    {% render_comment_list for item %}
    

    where you want the add comments form displayed:

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

    read the docs here and for customization read this page.

    as part of the docs:

    To specify the URL you want to redirect to after the comment has been posted, you can include a hidden form input called next in your comment form. For example:

    <input type="hidden" name="next" value="{% url my_comment_was_posted %}" />
    

    (edited for your example):

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