Django-如何制作upvote按钮?

时间:2019-03-14 08:45:13

标签: django django-models django-views

当前登录的用户最多可以投票x次。 修复它的想法是创建一个称为Vote的新模型。它将具有用于用户和产品的外键。当有人投票时,您可以查看并看到: 是否有具有该用户ID和产品ID的投票对象,如果存在,则不允许他再次投票;如果不是,那么您可以继续进行创建,只需增加total_votes即可。 但是实际上,我遇到了这个问题,无法解决它。

所以,有我的模型。py

from django.db import models
from django.contrib.auth.models import User

class Product(models.Model):
    title = models.CharField(max_length=255)
    pub_date = models.DateTimeField()
    body = models.TextField()
    url = models.TextField()
    image = models.ImageField(upload_to='images/')
    icon = models.ImageField(upload_to='images/')
    votes_total = models.IntegerField(default=1)
    hunter = models.ForeignKey(User, on_delete=models.CASCADE)

def __str__(self):
    return self.title

def summary(self):
    return self.body[:100]

def pub_date_pretty(self):
    return self.pub_date.strftime('%b %e %Y')

class Vote(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    voteproduct = models.ForeignKey(Product, on_delete=models.CASCADE)

还有我的views.py

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import Product, Vote
from django.utils import timezone

@login_required(login_url="/accounts/signup")
def upvote(request, product_id):
    if request.method == 'POST':
         product = get_object_or_404(Product, pk=product_id)
         product.votes_total += 1
         product.save()
         return redirect('/products/' + str(product.id))

更新:

class Vote(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)

class Meta:
    unique_together(('user', 'product'),)

views.py

if request.method == 'POST':
    product = get_object_or_404(Product, pk=product_id)
    try:
        Vote.objects.create(user=request.user, product=product)
        product.votes_total += 1
        product.save()
    except:
        product.save()
    return redirect('/products/' + str(product.id))

另外,我应该如何更改.html文件?我真的不明白发生了什么。期待收到您的来信。

{% for product in products.all %}
<div class="row pt-3">
<div class="col-2" onclick="window.location='{% url 'detail' product.id 
                                        %}';" style="cursor: pointer;">
<img src="{{ product.icon.url }}" class="img-fluid" />
</div>
<div class="col-7" onclick="window.location='{% url 'detail' product.id 
                                        %}';" style="cursor: pointer;">
<h1>{{ product.title }}</h1>
<p>{{ product.summary }}</p>
</div>
<div class="col-3">
    <a href="javascript:{document.getElementById('{% url 'upvote' 
    product.id %}').submit()}"><button class="btn btn-primary btn-lg btn- 
    block" name="btn1" value="upvote"><span class="oi oi-caret-top"> 
    </span> Upvote {{ product.votes_total }}</button></a>
</div>
</div>

<form id="get_redirect_url{{ product.id }}" action="{% url 'upvote' 
object.id %}" method="POST">
{% csrf_token %}
<input type="hidden" />
</form>

2 个答案:

答案 0 :(得分:1)

好吧,如果我是你,我会那样做:

#step 1  change votes_total from integerfield to manytomany field 
##models
class Product(models.Model):
    #other fields goes here
    votes_total = models.ManyToManyField(User, related_name="votes" ,)

#step 2 ; go to your views.py and copy paste the following :

from django.views.generic import RedirectView


class ProductVoteToggle(RedirectView):

    def get_redirect_url(self, *args ,**kwargs):

        obj = get_object_or_404(Product, pk=self.kwargs['pk'])
        url_ = obj.get_absolute_url() 
        user = self.request.user
        if user.is_authenticated():
            if user in obj.votes_total.all():
                # you could remove the user if double upvote or display a message or what ever you want here
                obj.votes_total.remove(user)
            else:
                obj.votes_total.add(user)

        return url_


   #step 3 : go to urls.py and paste add the following : 
    urlpatterns = [
        #other urls here
        path('vote/<int:pk>',ProductVoteToggle.as_view() , name="upvote"),
]
     #in your template remove that form and paste the following code : 
     <a href="{% url 'upvote' product.id %}>Click to vote<a/>

完成后,如果没有超过这里的错误,它应该可以工作,我会告诉你你在做什么错

答案 1 :(得分:0)

您的代码根本不使用Vote。在upvote()中,首先尝试为用户和产品创建Vote

try:
    Vote.objects.create(user=request.user, product=product)
    # consider using the name product instead of voteproduct

如果成功,则增加计数器。如果失败,请通知用户他们已经投票。

很显然,您需要在Vote中输入unique constraint

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