Hello fellow djangos,

I have a slight problem with my project, a simple solution is needed I
believe, but I couldn't figure it out.

I wanted to add a comment section for my blog posts.
Here is the code:

Models.py:

class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE,
related_name='comments')
    name = models.CharField(max_length=80)
    email = models.EmailField()
    body = models.TextField()
    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.name)


Views.py:

class PostDetailView(DetailView):
    model = Post

    def post_detail(request, slug):

        template_name = 'blog/post_detail.html'
        post = get_object_or_404(Post, slug=slug)
        comments = post.comments.filter(active=True)
        new_comment = None
        if request.method == 'POST':
            comment_form = CommentForm(data=request.POST)
            if comment_form.is_valid():
                new_comment = comment_form.save(commit=False)
                new_comment.post = post
                new_comment.save()
        else:
            comment_form = CommentForm()

        return render(request, template_name, {'post': post,
                                               'comments': comments,
                                               'new_comment': new_comment,
                                               'comment_form': comment_form
                                               })


Blog/post_detail.html:

{%extends 'blog/base.html'%}
{% load crispy_forms_tags %}
{%block content%}
    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{
object.author.profile.image.url}}" >
        <div class="media-body">

            <div class="article-metadata">
                <a class="mr-2" href="{% url 'user-posts'
object.author.username %}">{{ object.author }}</a>
                <small class="text-muted">{{
object.date_posted|date:"d F, Y" }}</small>

                {% if object.author == user %}
                    <div>
                        <a class="btn btn-primary btn-sm mt-1 mb-1"
href="{% url 'blog-home'%}"> Home </a>
                        <a class="btn btn-secondary btn-sm mt-1 mb-1"
href="{% url 'post-update' object.id %}"> Update </a>
                        <a class="btn btn-danger btn-sm mt-1 mb-1"
href="{% url 'post-delete' object.id %}"> Delete </a>
                    </div>

                {% endif %}
            </div>

            <h2 class="article-title">{{ object.title }}</h2>
            <p class="article-content">{{ object.content }}</p>
        </div>
    </article>
    {% for comment in comments %}
                     <div class="comments" style="padding: 10px;">
                        <p class="font-weight-bold">
                            {{ comment.name }}
                            <span class=" text-muted font-weight-normal">
                                {{ comment.created_on }}
                            </span>
                        </p>
                        {{ comment.body | linebreaks }}
                    </div>
    {% endfor %}
    <div class="col-md-8 card mb-4 mt-3">
        <div class="card-body">
            {% if new_comment %}
            <div class="alert alert-success" role="alert">
                Your comment is awaiting moderation
            </div>
            {% else %}
            <h3>Leave a comment</h3>
            <form method="post" style="margin-top: 1.3em;">
                {{ comment_form | crispy }}
                {% csrf_token %}
                <button type="submit" class="btn btn-primary
btn-lg">Submit</button>
            </form>
            {% endif %}
        </div>
    </div>
{% endblock content%}


And the urls.py:

urlpatterns = [
    path('', PostListView.as_view(), name='blog-home'),
    path('about/', views.about, name='blog-about'),
    path('showroom/', views.showroom, name='blog-showroom'),
    path('photos/', views.photos, name='blog-photos'),
    path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
    path('post/new/', PostCreateView.as_view(), name='post-create'),
    path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
    path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
    path('user/<str:username>', UserPostListView.as_view(), name='user-posts'),

]




The problem is following:
In the post detail page, I can not see the fields required for commenting,
I only see "Leave a comment" and submit button that does not work..
Any help would be nice

Cheers,
Luka

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMqindt2sqxVH1M7usu9sF0y84Lu7o6pRV27HVWUc_PTMu2NEg%40mail.gmail.com.

Reply via email to