1

I have written some code that allows users of a website to comment on photos in a picture gallery, using a form. However, when I test the code, no comments are displayed.

It would appear that Django is not processing the following code (from my HTML file for the photo gallery), given that 'Comment by' is not displayed on screen:

            {% for comment in comments %}
            <div class="comments" style="padding: 10px;">
                <p class="font-weight-bold">
                    <h4>Comment by</h4> {{ comment.user }}
                    <span class=" text-muted font-weight-normal">
                        {{ comment.created_on }}
                    </span>
                </p>
                {{ comment.body | linebreaks }}
            </div>
            {% endfor %}

This is my views.py code:

@login_required
def add_comment(request, image_id):
new_comment = None
template_name = 'add_comment.html'
image = get_object_or_404(Picture, id=image_id)
comment = image.comment.filter(active=True)
new_comment = None
# Comment posted
if request.method == 'POST':
    comment_form = CommentForm(data=request.POST)
    if comment_form.is_valid():
        # Create Comment object and don't save to database yet
        new_comment = comment_form.save(commit=False)
        # Assign the current post to the comment
        new_comment.post = post
        # Save the comment to the database
        new_comment.save()
else:
    comment_form = CommentForm()

context = {'comment_form': comment_form, 'image': image,'comment': comment, 'new_comment': new_comment,'comment_form': comment_form}

return render(request, template_name, context)

Does anyone have any suggestions as to how I can fix this, please?

EDIT: The code, which I have revised slightly since making this post, is at: https://github.com/EmilyQuimby/my_now_and_then. Any feedback appreciated.

Thank you.

Jeff

4
  • Is comments empty? Commented Mar 30, 2020 at 14:13
  • Hi Ger I don't know. I've edited my post above to include my views.py code. Does that tell you whether comments should be populating when I fill in the form? Thanks Commented Mar 30, 2020 at 14:57
  • You don't send any variable named comments to the django template, maybe misspelled for comment? Commented Mar 30, 2020 at 17:00
  • Thanks Sridhar. I've tried replacing 'comments' with 'comment', to no avail, but thanks anyway. Commented Mar 30, 2020 at 17:58

1 Answer 1

1

In your context you are not passing comments only comment to your HTML file. So the for loop in your template file {% for comment in comments %} can't find the variable comments to loop over. This means it does not enter the for loop to render your HTML.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Dean. I've just tried changing 'comments' to 'comment' in my views.py method, and I've also changed my related_name in models from 'comments' to 'comment'. My hope was that Django would realise that 'comments' in the plural of 'comment'. However, I'm still having the same issue. Is there anything else that you can think of that might fix the problem, please?
I checked your GitHub, the template syntax is incorrect. You have {% for comment in comment %}. These need to be different. For example: {% for x in comment %}
Thanks Dean. So would I just change all instances of comment to x, i.e. x.created_on, etc. Also, I'm currently getting an error message on the photo feed page - NoReverseMatch at /photo_feed/. Do you have any suggestions as to how I fix that, please?
No problem, yes you would. This is separate question, could you mark this as resolved if this answered your original question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.