NoReverseMatch error message - Can you help please?

I am putting together a website which has a photo gallery where users can add comments. When I go to the photo gallery page, I get the following error message:
NoReverseMatch at /photo_feed/ Reverse for ‘add_comment’ with arguments ‘(’’,)’ not found. 1 pattern(s) tried: [‘add_comment/$’]

The code for the relevant part of the HTML document is as follows:

        {% if not comments %}
        No comments
        {% endif %}
        {% for x in comment %}
        <div class="comments" style="padding: 10px;">
            <p class="font-weight-bold">
                <h4>Comment by</h4> {{ x.user }}
                <span class=" text-muted font-weight-normal">
                    {{ x.created_on }}
            {{ x.body | linebreaks }}
        {% endfor %}
    <div class="card-body">
        {% if new_comment %}
        <h2>Your comment has been posted.</h2>
        {% else %}
        <h3>Leave a comment</h3>
        <form action="{% url 'nowandthen:add_comment' %}" method="POST">
            {{ comment_form.as_p }}
            {% csrf_token %}
            <button type="submit" class="btn btn-primary  btn-lg">Submit</button>
        {% endif %}

The entry for add_comment is path(‘add_comment/<int: image_id>’, views.add_comment, name=‘add_comment’). Removing the int: image_id doesn’t fix the problem.

When I go into admin, no ids appear to have been generated for the photos. Could it be that the problem is that there are missing IDs? If so, how do I fix this?

The repository URL is

Thank you.


Your last paragraph does hit the right key item. If is blank (or null), then there’s no parameter to supply to the url and it can’t generate the reverse lookup properly. (You can verify this by manually adding an ID to an image and then retrying this page.)

As far as “why” no IDs are being generated, I’d take a look at the database to verify that the ID field has been created as an autoincrement primary key. Is this a new database being created for the app, or this is a pre-existing database on which you’re adding a Django app? If the former, then I’d try just recreating a new database to verify its structure. If the latter, you might have other work you need to do to make this right.

(Also, while being able to examine your code was really helpful in trying to diagnose this issue, you really want to make sure that “secret” data never gets published into a public github repo.)

1 Like

Thank you Ken

The image field was created in a model called Pictures as follows:

image = models.ImageField(upload_to=‘shared_pics’, unique=True)

I also included it as a foreign key in the Comments model:

image = models.ForeignKey(Picture, on_delete=models.CASCADE, related_name=“comments”)

Should that automatically generate unique IDs?

With regards to the GitHub URL, thank you for the advice. As I’ve already posted the link, I will leave it up, but I will change the admin login details before the site goes live (at the moment, it’s just on localhost).


Thank you Ken. Apologies if I am replying twice - I thought I’d replied earlier, but my message seems to have disappeared!

‘Image’ was created in the Picture model using image = models.ImageField(upload_to=‘shared_pics’, unique=True) and in the Comment model using image = models.ForeignKey(Picture, on_delete=models.CASCADE, related_name=“comments”). Should it be automatically generating ids?

Thank you for the feedback about posting URLs.


Ken’s right.

You seem to be using SQLite. This db doesn’t enforce types on its columns, so it’s possible you created comment objects with id = ' ' in Django, even though you are using the implicit ID key. Check all your code for creating items.

Thanks Adam

As far as I know, the only code for creating items I used is the code in Models:

image = models.ImageField(upload_to=‘shared_pics’, unique=True) in the Pictures model,


image = models.ForeignKey(Picture, on_delete=models.CASCADE, related_name=“comment”)

in the Comments model.

I’d assumed that that would result in each item being labelled. If that isn’t the case, what code can I add to create ID keys?


Check the database itself - make sure that the ID field is an autoincrement column (or whatever the SQLite equivalent would be.) SQLite should be doing that automatically.