django表单不显示在页面上

时间:2018-01-15 21:45:56

标签: 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>

views.py:

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)
      context ={
        'post':post
         }

     return render(request,'post/comment.html',context)

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

表单未在页面上显示有什么问题 我尝试了很多,但有困难

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)

表单未在页面上显示有什么问题 我尝试了很多,但有困难 网址:

    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"),

表单未在页面上显示有什么问题 我尝试了很多,但有困难

no form in the page

页面中没有任何表格

1 个答案:

答案 0 :(得分:1)

看起来你早早从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})