1

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.

Sgt.Fault
  • 45
  • 1
  • 6
  • while not part of the solution you should be cautious of using `if request.POST:`, rather use `if request.method == 'POST':` – dting Apr 14 '11 at 03:10

1 Answers1

1

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 %}
dting
  • 38,604
  • 10
  • 95
  • 114
  • Sorry I didn't clarify this but what I wanted was for someone to post a comment about an item and have the comment appear on the same page. (item_detail.html). In the above answer, (r'^comments/', include('django.contrib.comments.urls')) would send the user to a different page if I'm right and I don't want to do that. I want to use their framework but I want everything redirected to the same webpage. Is there a way to do this apart from creating a custom comment class? – Sgt.Fault Apr 14 '11 at 02:24
  • Thanks, worked like a charm. Just need to get rid of the preview page and I'm set – Sgt.Fault Apr 14 '11 at 02:45