HTML code not being read

32 views
Skip to first unread message

Jeff Waters

unread,
Mar 30, 2020, 10:13:49 AM3/30/20
to Django users
Hi

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 %}

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

Thank you.

Jeff

Luqman Shofuleji

unread,
Mar 30, 2020, 10:24:26 AM3/30/20
to django...@googlegroups.com
The problem might not be from the HTML template but the View.
All the HTML elements within your for loop will not display if there are no records to loop through in "comments"

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4666d2ec-e4d8-492d-8203-03dfecd29d6a%40googlegroups.com.

LGBS fine soul coders

unread,
Mar 30, 2020, 10:26:54 AM3/30/20
to django...@googlegroups.com
Have you all ready use a content that define your content

Jeff Waters

unread,
Mar 30, 2020, 10:51:21 AM3/30/20
to Django users
Hi

Thanks for getting back to me.

This is from my views.py:

@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 that look correct to you?

Jeff

Jeff Waters

unread,
Mar 30, 2020, 11:04:05 AM3/30/20
to Django users
Hi

I'm not sure what you mean by 'Have you all ready use a content that define your content'.

I'm new to Django, so am not familiar with all of the terminology yet.

Can you elaborate please?

Thanks

Jeff

Ryan Nowakowski

unread,
Mar 30, 2020, 1:48:10 PM3/30/20
to django...@googlegroups.com, Jeff Waters
Your view context uses 'comment'(singular), while your template uses
'comments'(plural).  I'd chnage your view context to match the template.

Luqman Shofuleji

unread,
Mar 30, 2020, 2:26:42 PM3/30/20
to django...@googlegroups.com
What you passed to the context in your views.py is 'comment', but you are looping through 'comments' in the template

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Jeff Waters

unread,
Mar 30, 2020, 2:40:54 PM3/30/20
to Django users
Thanks guys

I previously had 'comments' in the plural in views.py, but had the same problem.

Am I right in thinking that Django recognises that 'comments' is the plural of 'comment', so if it knows from the view what a comment is, it knows what comments are?

Thanks,

Jeff

Aly_34_04 MR_34_04

unread,
Mar 30, 2020, 4:06:32 PM3/30/20
to Django users
show me your views 

Jeff Waters

unread,
Mar 30, 2020, 5:02:04 PM3/30/20
to Django users
I've modified my code a bit to try (unsuccessfully!) to get this working.

My views.py is now as follows:

@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.comments.filter(active=True)


new_comment = None
# Comment posted
if request.method == 'POST':

comment_form = CommentForm(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 = {'image': image,'comment': comment, 'new_comment': new_comment,'comment_form': comment_form}

return render(request, template_name, context)

My html code for the gallery is now:

<h2>comments</h2>
{% if not comments %}
No comments
{% endif %}
{% for comment in comment %}


<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 %}

The problem I now have is that I now get the following error message: Reverse for 'add_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['add_comment/(?P<image_id>[0-9]+)$']

Any suggestions would be much appreciated.

Thanks

Jeff

Luqman Shofuleji

unread,
Mar 30, 2020, 5:18:21 PM3/30/20
to django...@googlegroups.com
Let's see your url.py

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Ryan Nowakowski

unread,
Mar 30, 2020, 5:54:14 PM3/30/20
to Jeff Waters, Django users
On 3/30/20 1:03 PM, Jeff Waters wrote:
@login_required
def add_comment(requestimage_id):
    new_comment = None
    template_name = 'add_comment.html'
    image = get_object_or_404(Picture, id=image_id)
    comment = image.comments.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 = {'image': image,'comment': comment, 'new_comment': new_comment,'comment_form': comment_form}
    return render(request, template_name, context)

Allaberdi Yazhanow

unread,
Mar 31, 2020, 2:25:33 AM3/31/20
to django...@googlegroups.com
Did you connect your template folder on settings.py ?

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages