Search code examples
djangodjango-comments

Use django comments on article's comment page (/a/2/comments/)


I have made a blog with Django with articles (like so: mysite.com/a/article_id/) and would like users to be able to comment on the article's comment page (i.e: mysite.com/a/article_id/comments/)

So far I haven't had much success. It seems that the article_id in the url is blocking somehow the comments app.

This is my url.py:

from django.conf.urls import patterns, include, url
from django.contrib.auth.views import login, logout

urlpatterns = patterns('blogengine.views',
    url(r'^$', 'get_posts', name='index'),
    url(r'^write/', 'write_post', name='write'),
    url(r'^a/(?P<post_id>\d+)/$', 'detail'),
    url(r'^a/(?P<post_id>\d+)/comments/$', 'detail_comments'),
    url(r'^a/(?P<post_id>\d+)/comments/', include('django.contrib.comments.urls')),
)

These are my views - views.py:

def detail_comments(request, post_id):
    p = get_object_or_404(Post, pk=post_id)
    return render_to_response('blogengine/detail_comments.html', {'post': p},
    context_instance=RequestContext(request))

And this is my template detail_comments.html

{% block content %}
{% load comments %}
{% get_comment_form for post as form %}

<form action="/a/{{ post.id }}/comments/post/" method="post">
    {% csrf_token %}
{{ form.content_type }}
{{ form.object_pk }}
{{ form.timestamp }}
{{ form.security_hash }}
<p style="display:none"><label for="id_honeypot">Leave blank</label>{{ form.honeypot }}</p>
<p>
    <label for="id_comment">Comment</label>
    {{ form.comment }}
</p>
<p><input type="submit" name="post" value="Post &rarr;" /></p>
</form>
{% endblock %}

(Oh and this is kind of obvious but the comments app is installed in settings.py)

If the form action is set to {% comment_form_target %}, like suggested in the docs, django throws this error:

NoReverseMatch at /a/2/comments/
Reverse for 'django.contrib.comments.views.comments.post_comment' with arguments '()' and keyword arguments '{}' not found.

I tried "hacking" my way out by replacing it with this /a/{{ post.id }}/comments/post/ which works to display the page but then if I try to post a comment, django throws a different error:

TypeError at /a/2/comments/post/
post_comment() got an unexpected keyword argument 'post_id'

Is there a way to get the comments app to ignore the id_post? Or another way to do this?

Thanks.


Solution

  • Ok so I solved my problem by simply doing what the docs say. I imported the comments like so:

    url(r'^comments/', include('django.contrib.comments.urls')),
    

    And kept this url pointing to my detail_comments view which displays the comment list and form:

    url(r'^a/(?P<post_id>\d+)/comments/$', 'detail_comments'),
    

    So basically the processing happens at /comments/ but the user interacts with this page: /a/post_id/comments/

    The only problem I had was that the Django comments app automatically redirected the user to a success page after posting a comment. I solved this by setting a "next" hidden field in the form indicating the current page.