Python django表单未显示在页面上
表单未在页面上显示问题所在 我试了很多,但有困难 comment.html:Python django表单未显示在页面上,python,django,Python,Django,表单未在页面上显示问题所在 我试了很多,但有困难 comment.html: <form method="POST" class="post-form">{% csrf_token %} {{ form.as_p }} <button type="submit" class="save btn btn-default">Save</button> </form> 表单未在页面上显示问题所在 我试了很多,但有困难 f
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Save</button>
</form>
表单未在页面上显示问题所在
我试了很多,但有困难
forms.py:
from django import forms
from .models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('name','email','body','website',)
models.py:
class Comment(models.Model):
post= models.ForeignKey(Post,related_name='comments',on_delete=models.CASCADE)
name = models.CharField(max_length=80)
website = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
class Meta:
ordering =('created',)
def __str__(self):
return 'Comment by {} on {}'.format(self.name,self.post)
表单未在页面上显示问题所在
我试了很多,但有困难
网址:
从django.conf.url导入url
从…起导入视图
URL模式=[
#url(r'^$',PostListView.as_view(),name='blog'),
url(r'^$',视图.发布列表视图,name='发布列表视图'),
#url(r'^(?P\d+/$),PostDetailView.as_view(),name='post_detail'),
url(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[-\w]+)/$”,视图。发布详细信息视图,name=“发布详细信息视图”),
]
表单未在页面上显示问题所在
我试了很多,但有困难
页面中没有表单看起来您是从
post\u detail\u视图
提前返回的。试试这个:
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)
comments = post.comments.filter(active=True)
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()
else:
form = CommentForm()
return render(request,'post/comment.html',{'post':post,
'comments': comments,
'form':form})
from django.conf.urls import url
from . import views
urlpatterns = [
#url(r'^$',PostListView.as_view(),name='blog'),
url(r'^$',views.post_list_view,name='post_list_view'),
#url(r'^(?P<pk>\d+)/$',PostDetailView.as_view(),name='post_detail'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$',views.post_detail_view,name="post_detail_view"),
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)
comments = post.comments.filter(active=True)
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()
else:
form = CommentForm()
return render(request,'post/comment.html',{'post':post,
'comments': comments,
'form':form})