简体   繁体   中英

like and comment section: in django

I'd like to put like and comment section in my blog project with some info like no. of likes and comments and also show user who comment on it in django project but don't know how. 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:

{% 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

class PostListViews(ListView):
    model = Post
    template_name = 'userpost/posts.html'
    context_object_name = 'post'
    ordering = ['-date_posted']
    paginate_by = 7

models.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. This will allow you to create Comment objects, then you will be able to stock them in your database and display them.

For the view, I prefer Function Based View, it will be easier for you.

And you have to create a ModelForm. To simplify creation of comments you should create an other html page.

I hope it was clear, Do not hesitate if you have any questions !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM