Form not displaying

28 views
Skip to first unread message

Jeff Waters

unread,
Mar 29, 2020, 10:12:25 PM3/29/20
to Django users
Apologies if I'm posting this twice - my original message isn't displayed in the list of messages.

I am creating a website using Django. I'd like to give users the chance to comment on pictures they have posted. I have created a comment model and a comment form, and put the following code into the HTML document for the photo gallery:

<h3>Leave a comment</h3>
<form method="post" style="margin-top: 1.3em;">
{{ comment_form.as_p }}
{% csrf_token %}
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
</form
However, the form is not displaying - there is nothing between 'Leave a comment' and the submit button on the page. I don't understand this as my form in forms.py appears to be configured correctly:

class CommentForm(forms.ModelForm):
body = forms.CharField(help_text="What is your comment?", widget=forms.TextInput(attrs={'size': '1000'}),
required=True)

class Meta:
model = Comment
fields = ('body',)
def as_p(self):
# Returns this form rendered as HTML <p>s.
return self._html_output(
normal_row='<p%(help_text)s<p></p>%(field)s</p>',
error_row='%s',
row_ender='</p>',
help_text_html=' <span class="helptext">%s</span>',
errors_on_separate_row=True)`
So does my model in models.py:

class Comment(models.Model):
COMMENT_MAX_LENGTH = 1000
image = models.ForeignKey(Picture, on_delete=models.CASCADE, related_name="comments")
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
body = models.TextField(max_length=COMMENT_MAX_LENGTH)
created_on = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=False)

class Meta:
ordering = ['created_on']

def __str__(self):
return 'Comment {} by {}'.format(self.body, self.user)

When I go into the source code on the site, it shows that the form is hidden. Also, when I change comment_form.as_p to something random, no error messages are generated. It's like something is causing Django to skip past that bit of code.

I'd really appreciate any suggestions anyone could please offer.

Thanks

Jeff

Ernest Thuku

unread,
Mar 30, 2020, 2:09:39 AM3/30/20
to Django users

Can you display your views please?

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac7ac525-9bc4-4043-a142-b97b1d3d0e5c%40googlegroups.com.

Jeff Waters

unread,
Mar 30, 2020, 4:54:52 AM3/30/20
to Django users
Sure. It's as follows:

@login_required
def add_comment(request, image_id):
template_name = 'add_comment.html'
image = get_object_or_404(Picture, id=image_id)
comments = 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()

return render(request, template_name, {'image': image,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})

Thanks

Jeff

Ernest Thuku

unread,
Mar 30, 2020, 5:15:10 AM3/30/20
to Django users

In the views why have you used templane_name while you not using class based views...try and change that to render the template using request

--
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+unsubscribe@googlegroups.com.

Jeff Waters

unread,
Mar 30, 2020, 5:58:16 AM3/30/20
to Django users
Thanks Ernest.

I'm not sure I follow (forgive me, I am very new to Django).

Given that template_name is defined earlier in the method as 'add_comment.html', surely what I've written is equivalent to:

return render(request, add_comment.html, {'image': image,


'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})

What should the code be instead?

Thanks

Jeff

Ernest Thuku

unread,
Mar 30, 2020, 6:26:31 AM3/30/20
to Django users

There in your template...that closing form tag...is it a typing error?I can't see the second closing tag

On Mar 30, 2020 13:23, "Ernest Thuku" <ernes...@gmail.com> wrote:

Try this

--
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+unsubscribe@googlegroups.com.

Ernest Thuku

unread,
Mar 30, 2020, 6:28:03 AM3/30/20
to Django users

Try this

On Mar 30, 2020 12:58, "Jeff Waters" <wate...@gmail.com> wrote:
--
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+unsubscribe@googlegroups.com.
IMG20200330132109.jpg

Jeff Waters

unread,
Mar 30, 2020, 7:45:05 AM3/30/20
to Django users
Will do, thanks
Reply all
Reply to author
Forward
0 new messages