简体   繁体   English

如何在 django 中基于 class 的列表视图中添加点赞按钮

[英]How to add like button in class based List view in django

I have added like button in detail view but could't able make the logic to add like button in List View.我已经在详细视图中添加了点赞按钮,但无法使逻辑在列表视图中添加点赞按钮。 I have commented out the logic which I have used in Detail View below which is failed to create like button in List View.我已经注释掉了我在下面的详细视图中使用的逻辑,该逻辑未能在列表视图中创建类似按钮。 Can anyone suggest me to do the best way, can somebody help me with this please.任何人都可以建议我做最好的方法,有人可以帮助我吗?

model.py model.py

Codes in models.py models.py 中的代码

class AlbumVoteManager(models.Manager):

    def get_vote_or_unsaved_blank_vote(self,album,user):
        try:
            return Vote.objects.get(album=album,user=user)

        except ObjectDoesNotExist:
            return Vote(album=album,user=user)

class AlbumVote(models.Model):
    UP = 1
    DOWN = -1
    VALUE_CHOICE = ((UP, "👍️"),(DOWN, "👎️"),)


    like = models.SmallIntegerField(null=True, blank=True, choices=VALUE_CHOICE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    album = models.ForeignKey(Album, on_delete=models.CASCADE)
    voted_on = models.DateTimeField(auto_now=True)
    created_on = models.DateTimeField(auto_now_add=True)

    objects = AlbumVoteManager()

    class Meta:
        unique_together = ('user', 'album')

form.py表格.py

Codes in form.py form.py 中的代码

class AlbumVoteForm(forms.ModelForm):

    class Meta:
        model = AlbumVote
        fields = ['like']

views.py视图.py

Codes in views.py views.py 中的代码

class AlbumListView(ListView):
    model = Album
    paginate_by = 5

    # def get_context_data(self,**kwargs):
    #     ctx = super().get_context_data(**kwargs)

    #     if self.request.user.is_authenticated:
    #         vote = 
AlbumVote.objects.get_vote_or_unsaved_blank_vote(album=self.object_list.object, user = self.request.user)
    #         if vote.id:
    #             vote_url = reverse('music:album_vote_update', kwargs={'album_id':vote.album.id,'pk':vote.id})
    #         else:
    #             vote_url = reverse('music:album_vote_create', kwargs={'album_id':self.object_list.object.id})


    #         vote_form = AlbumVoteForm(instance=vote)
    #         ctx['vote_form'] = vote_form
    #         ctx['vote_url'] = vote_url
    #     return ctx

album_list.html专辑列表.html

Codes in album list view.专辑列表视图中的代码。

  <div class="container">
    {% if object_list %}
        {% for album in object_list %}

        <div class="card my-3">
            <div class="card-header">
              Featured
            </div>
            <div class="card-body">
              <h5 class="card-title">

                    <a href="{% url 'music:detail_view' pk=album.id %}"><strong>{{album.title}}</strong></a>

              </h5>
              <p class="card-text">{{album.discription}}.</p>
              <a href="#" class="btn btn-primary">Go somewhere</a>
            </div>
          </div>

Well this can be implemented by passing a unique id to the buttons while the classes are the same for all on the template.这可以通过将唯一的 id 传递给按钮来实现,而模板上的所有类都相同。 You can make reference on this article on Medium here which implemented something similar using AJAX您可以在此处参考 Medium 上的这篇文章,该文章使用 AJAX 实现了类似的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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