There is no problem in the code. Look again at the quote and typos in the 
form class. and do a trial with this view:

from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import CommentForm


def my_view(request):
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/thanks/')
    else:
        form = CommentForm()
    return render(request, 'index.html', {'comment_form': form})





30 Mart 2020 Pazartesi 05:12:30 UTC+3 tarihinde Jeff Waters yazdı:
>
> 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

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/989ca9da-02bb-4596-ad34-dfd2ded2e6a1%40googlegroups.com.

Reply via email to