django - Django Form 没有显示在模板文件中,也没有出现任何错误
问题描述
我是 Django 的新手,在我的详细帖子页面下方创建了一个博客应用程序并尝试显示评论表单,但即使我也使用了 csrf 并且在运行服务器时没有收到任何错误,我也没有收到表单。
然而,按钮和评论等其他内容显示在 Web 浏览器中。
1.models.py
######Model related to comment
# We required to create a Model to save comments
# We required to create a model based form to submit comments
# We required to create a view function to process comments and save to the database
# We have to edit post_detail.html to display list of comments and add a form to submit
# comments
class Comment(models.Model):
post=models.ForeignKey(Post,related_name='comments',on_delete=models.DO_NOTHING)
name=models.CharField(max_length=32)
email=models.EmailField()
body=models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
#now order we can define.
class Meta:
ordering=('-created',)
#now let me take str if any person want to display comment object.
def __str__(self):
return 'Commented by {} on {}'.format(self.name,self.post)
#Means if any person want to display my comment object sir simply return o/p in above form :)
#Now after creating comment model we need to run migration and migrate and need to register inside admin.py
from blog.forms import CommentForm
def post_detail_view(request,year,month,day,post):
post = get_object_or_404(Post,slug=post,status='Published',publish__year=year,
publish__month=month,
publish__day=day)
#now to implement comment we will use related name means post.comments (comment related to this post)
comments=post.comments.filter(active=True)#i got all comment related to post.
csubmit=False
if request.method=='POST':
form=CommentForm(request.POST)
if form.is_valid():
new_comment=form.save(commit=False)
new_comment.post=post
new_comment.save()
csubmit=True
else:
#if not POST then we need to display form.:)
form=CommentForm()
###lets visualize above 3 lines user will submit only 3 field data (name email and body but for which post that we are associating here.)
return render(request,'blog/post_detail.html',{'post': post,form:'form',csubmit:'csubmit','comments':comments})
3.post detail template
在这里,我试图显示评论表单。
<!DOCTYPE html>
{% extends 'blog/base.html'%}
{%block title_block %}
{{post.title}}
{%endblock%}
{%block content%}
<h1>{{post.title}}</h1>
<p id='date'>Published on {{post.publish}} by {{post.author|title}}</p>
{{post.body|linebreaks}}<br>
<div class="container1" align="center">
<!--####Now here below to detail post i'll enter email button href which will open post and email form.-->
<a href="/{{post.id}}/share" class="btn btn-lg btn-success">Share Post By Email</a>
<!--Note: target_blank will open page in new tab.-->
</div>
<!--#######adding comment section below to post######-->
{% with comments.count as comment_count %}
<h2>{{comments_count}} Comment{{comments_count|pluralize}}</h2>
{%endwith%}
<!--#now lets display comments:)-->
{%if comments%}
{%for comment in comments%}
<p id='ch'> comment {{forloop.counter}} by {{comment.name}} on {{comment.created}}</p>
<div class="commentbody">{{comment.body|linebreaks}}</div><hr>
{%endfor%}
{%else%}
<p>There is no Comment Yet!!You are the first to comment on this..</p>
{%endif%}
<!--###Now if comment is Submitted then we need to display form to end user.-->
{%if csubmit%}
<h2>Hey!,Your commented has submitted successfully. </h2>
{%else%}
<!-- <p id="submit1">Please submit your comment.</p>-->
<form method="post">
{{form.as_p}}
{%csrf_token%}
<input type="submit" name="" class='btn btn-lg btn-success' value="Submit Your Comment">
</form>
{%endif%}
{%endblock%}
4.forms.py
from django import forms
class EmailSendForm(forms.Form):
name=forms.CharField()
email=forms.EmailField()
to=forms.EmailField()
comments=forms.CharField(required=True, widget=forms.Textarea)
#here comment is not manadatory so we are taking required=False and comment can be of any size that is why we are taking text ared.
from blog.models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model=Comment
fields=('name','email','body')
解决方案
推荐阅读
- python - 如何在折线图上写出精确的点值?
- c# - 是否可以在不转换为 List 的情况下对 IQueryable 进行索引?
- lets-encrypt - 如何在 LetsEncrypt 的基于 ECDSA 的证书中保留 KeyUsage 和 BasicConstraints 的值?
- xml - 表达式必须对节点集求值是什么意思?
- python - Django 错误:“WSGIRequest”对象没有用于评论的属性“post”
- wordpress - 多部分 Wordpress 帖子
- ios - 广告未展示,但测试广告正确展示,也提出了请求,但没有展示
- android - ViewModel 作为片段构造函数的参数
- python - uWSGI 编码错误“ascii”编解码器无法在位置 7 编码字符“\xe9”:序数不在范围内(128)
- c - 在 C 中使用指针反转字符串(必须使用无符号 *char)