[英]like and comment section: in django
I'd like to put like and comment section in my blog project with some info like no.我想在我的博客项目中添加喜欢和评论部分,其中包含一些信息,例如 no。 of likes and comments and also show user who comment on it in django project but don't know how.喜欢和评论,还显示在 django 项目中对其发表评论但不知道如何评论的用户。 its a simple blog so i just want a like button and a comment button, above that its need to show how much likes and comment that post have and at least need to show one comment below that just like a normal blog have her is my html code:它是一个简单的博客,所以我只想要一个喜欢按钮和一个评论按钮,上面需要显示该帖子有多少喜欢和评论,并且至少需要在下面显示一条评论,就像普通博客一样拥有她是我的 html代码:
{% for post in post %}
<article class="media mt-4">
<div class="media-body">
<img class="rounded-circle article-img" id="dp" src="{{ post.author.profile.image.url }}">
<div class="article-metadata">
<a class="mr-2" href="{% url 'Love Travel-User' post.author.username %}">{{ post.author }}</a>
<small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
</div>
<h2><a class="article-title" href="{% url 'User-Posts-Details' post.id %}">{{ post.title }}</a></h2>
<img src="{{ post.img.url }}" class="article-img">
<p class="article-content">{{ post.content }}</p>
</div>
</article>
{% endfor %}
views.py视图.py
class PostListViews(ListView):
model = Post
template_name = 'userpost/posts.html'
context_object_name = 'post'
ordering = ['-date_posted']
paginate_by = 7
models.py模型.py
class Post(models.Model):
title= models.CharField(max_length=100)
img = models.ImageField(upload_to='pics')
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author= models.ForeignKey(User,on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('User-Posts-Details', kwargs={'pk': self.pk})
any help is appreciated任何帮助表示赞赏
I will not give the code but I will explain how to do it.我不会给出代码,但我会解释如何去做。
I will help you with the comments part.我会在评论部分帮助你。
You have to create a model Comment link with a Foreign key to Post model.您必须创建一个带有外键的 model 评论链接才能发布 model。 This will allow you to create Comment objects, then you will be able to stock them in your database and display them.这将允许您创建 Comment 对象,然后您将能够将它们存储在数据库中并显示它们。
For the view, I prefer Function Based View, it will be easier for you.对于视图,我更喜欢基于 Function 的视图,这对您来说会更容易。
And you have to create a ModelForm.你必须创建一个 ModelForm。 To simplify creation of comments you should create an other html page.为了简化注释的创建,您应该创建另一个 html 页面。
I hope it was clear, Do not hesitate if you have any questions !我希望它很清楚,如果您有任何问题,请不要犹豫!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.