tsujin
tsujin

Reputation: 787

How To Fix "NoReverseMatch" in django blog comments?

I've tried looking at quite a few posts first, and none of them seem to have an answer to my solution (at least not an obvious one). I'm still very new to Django, so I'm still getting a hang of how everything relates (in terms of the models, views, forms, etc). The blog was originally a carbon-copy of the djangogirls blog from their tutorial, but I wanted to extend it by adding comments from another tutorial on the web. I ran into an error that I couldn't figure out afterwards.

Here's the source code for the comments:

forms.py

class CommentForm(forms.ModelForm):
class Meta:
    model = Comment
    exclude = ["post"]

views.py

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)

    comments = Comment.objects.filter(post=post)
    d = dict(post=post, comments=comments, form=CommentForm(),
        user=request.user)
    d.update(csrf(request))
    return render_to_response('blog/post_detail.html', d)


def add_comment(request, pk):
    """Add a comment to a post."""
    p = request.POST
    if p.has_key("body") and p["body"]:
        author = "Anonymous"
        if p["author"]: author = p["author"]

        comment = Comment(post=Post.objects.get(pk=pk))
        cf = CommentForm(p, instance=comment)
        cf.fields["author"].required = False

        comment = cf.save(commit=False)
        comment.author = author
        comment.save()

    return redirect("dbe.blog.views.post_detail", args=[pk])

models.py

class Comment(models.Model):
    created_date = models.DateTimeField(auto_now_add=True)
    author = models.CharField(max_length=60)
    body = models.TextField()
    post = models.ForeignKey(Post)

def __unicode__(self):
    return unicode("%s: %s" % (self.post, self.body[:60]))

class CommentAdmin(admin.ModelAdmin):
    display_fields = ["post", "author", "created_date"]

url pattern (urls.py):

url(r'^add_comment/(\d+)/$', views.post_detail, name='add_comment')

post_detail.html:

<!-- Comments -->
{% if comments %}
    <p>Comments:</p>
{% endif %}

{% for comment in comments %}
    <div class="comment">
        <div class="time">{{ comment.created_date }} | {{ comment.author }}</div>
        <div class="body">{{ comment.body|linebreaks }}</div>
    </div>
{% endfor %}

<div id="addc">Add a comment</div>
<!-- Comment form -->
<form action="{% url add_comment post.id %}" method="POST">{% csrf_token %}
    <div id="cform">
        Name: {{ form.author }}
        <p>{{ form.body|linebreaks }}</p>
    </div>
    <div id="submit"><input type="submit" value="Submit"></div>
</form>

{% endblock %}

The error is highlighting this line in post_detail.html:

<form action="{% url add_comment post.id %}" method="POST">{% csrf_token %}

And the error itself says:

NoReverseMatch at /post/1/
Reverse for '' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

And the traceback from django is insanely unwieldy, but it first points back to this line in views.py:

    return render_to_response('blog/post_detail.html', d)

Upvotes: 3

Views: 153

Answers (1)

Paul Lo
Paul Lo

Reputation: 6148

If you are using Django newer than 1.4, you need to quote your add_comment:

<form action="{% url 'add_comment' post.id %}" method="POST">

Upvotes: 2

Related Questions