문제

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.

도움이 되었습니까?

해결책 2

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.

다른 팁

The error message is pretty unambiguous: django.contrib.comments.views.post_comment does not take a post_id argument, so it throws.

As the comments views do not need nor want the argument, why not just leave it out?

You should be able to modify the URL route not to capture the post_id at all (although at the cost of consistency) like so:

    url(r'^a/(?:\d+)/comments/', include('django.contrib.comments.urls')),

or simply

    url(r'^a/\d+/comments/', include('django.contrib.comments.urls')),

Note that there's really no point in having this kind of nesting at this point anyway if it's just going to be ignored, so you could simplify it to:

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

Granted, this doesn't look as pretty and pseudo-RESTful without the vestigial prefix, but there's really no point in having it there if you're just going to ignore it.

The other thing you could do would be to wrap all the views the comments app provides so they throw a 404 if the post_id is invalid, but that seems overkill.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top