Blog project help

18 views
Skip to first unread message

Luka Nik

unread,
Mar 18, 2020, 12:27:49 PM3/18/20
to django...@googlegroups.com
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

Python Programming

unread,
Mar 18, 2020, 1:01:03 PM3/18/20
to django...@googlegroups.com

‫‪Luka Nik‬‏ <‪luka...@gmail.com‬‏> در تاریخ چهارشنبه ۱۸ مارس ۲۰۲۰ ساعت ۱۹:۵۷ نوشت:‬
--
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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMqindt2sqxVH1M7usu9sF0y84Lu7o6pRV27HVWUc_PTMu2NEg%40mail.gmail.com.

Luka Nik

unread,
Mar 18, 2020, 1:07:31 PM3/18/20
to django...@googlegroups.com
Yes, I used that, but it's not working for me.
Actually I combined two projects, but, I think my blog/post-detail is not working

sanka nanaji

unread,
Mar 18, 2020, 2:12:19 PM3/18/20
to django...@googlegroups.com
I cannot see forms/serializers here. You have to make a modelserializer/form in your serializers.py/form.py file and write class meta in that and put comment fields in model user.
I would suggest you to go look in the document.
Thank you

Motaz Hejaze

unread,
Mar 18, 2020, 5:43:26 PM3/18/20
to django...@googlegroups.com
where is your forms.py file ???
you have to write the comments form .. 

using forms.Form
write field by field

or using forms.modelform
class meta : model = Comment



Reply all
Reply to author
Forward
0 new messages