简体   繁体   English

喜欢和评论部分:在 django

[英]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.

相关问题 登录后重定向到注释部分在Django中不起作用 - redirecting to comment section after login is not working in django 为每篇文章Django创建单独的评论部分 - Creating separate comment section per article Django Django 在帖子提要上添加评论部分 - Django add comment section on posts feed 使用引导带在注释部分插入新行(Python - Django) - Using Boot Strap To Insert New Rows In Comment Section (Python - Django) 在评论部分将单个用户的个人资料链接到用户-Django - link individual user's profile to user in comment section - Django 在注释部分放置断点或换行符(Python - Django) - Putting break points or line breaks in the comment section (Python - Django) 如何在 Django 中为帖子模型创建评论、赞、标签、报告等? - How to create comment, like, tag, report, and so on for a post model in Django? 我在我的 Django 项目中创建了一个评论部分,我想将用户重定向到他们在发布评论后刚刚评论的同一帖子 - I created a comment section in my Django project and i want to redirect users to that same post they just commented on after they posted the comment django 博客中的评论部分不会显示在每个单独的帖子下? - Comment section in django blog won't show up under each individual post? flask评论区怎么删除评论 - How to delete a comment from the comment section in flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM