Ahmed Lotfy
Ahmed Lotfy

Reputation: 3

django The form does not show on the page

The form does not show on the page what is the problem I tried a lot but there was difficulty

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

The form does not show on the page what is the problem I tried a lot but there was difficulty

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)

The form does not show on the page what is the problem I tried a lot but there was difficulty urls:

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

]

The form does not show on the page what is the problem I tried a lot but there was difficulty

no form in the page

no form in the page

Upvotes: 0

Views: 44

Answers (1)

Stuart Dines
Stuart Dines

Reputation: 758

It looks you are returning from post_detail_view early. Try this:

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

Upvotes: 1

Related Questions