如何检查用户是否已经通过模板或视图功能django评分

rdlzhqv9  于 2022-12-27  发布在  Go
关注(0)|答案(1)|浏览(151)

因此,在list_detail. html中,我包含了create_star. html文件,其中放置了星级评定表。
现在我只能给一个用户的帖子打分一次(这是我想要的),但是如果一个用户已经打分了,那么它就会进入else条件(进入create_star. html文件)
但我想做的事情是,如果用户已经评级。在post_detail. html我想它显示例如,如果用户评级5,我想显示5星的地方张贴星级形式(不可编辑)在list_detail. html页面。但不知道我该如何处理。
在我的模型中

class List(models.Model):
    title = models.CharField(max_length=120)
    genre = models.ManyToManyField('Genre')
    creator = models.ForeignKey(User,on_delete=models.SET_NULL,blank=True, null=True)
    posted = models.DateTimeField(auto_now_add=True)
    content = RichTextField(null=True,default=' ')
    type = models.CharField(max_length=10,default="Movie")
    spoiler_choices = [(False, 'No'),(True, 'Yes')]
    spoiler = models.BooleanField(default=False,null=True, choices = spoiler_choices)
    slug = models.SlugField(max_length= 300,null=True, blank = True, unique=True)

    def __str__(self):
        return f'{self.title}|{self.creator}'
    def save(self, *args, **kwargs):
        self.slug = slugify(self.title + str(self.posted))
        super(List,self).save(*args, **kwargs)

class Starr(models.Model):
    user = models.ForeignKey(User,models.CASCADE)
    list = models.ForeignKey(List,models.CASCADE)
    rate = models.PositiveIntegerField(default=0)

    class Meta:
        unique_together = ["user", "list"]

    def __str__(self):
        return self.user.username + " | " + self.list.title + " | " + str(self.rate)

在我看来

class TheirDetailView(DetailView):
    model = List
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        modell = Review.objects.all()
        starr = Starr.objects.all()
        context["starr"] = starr
        context["modam"] = modell
        return context

def create_starr(request):
    context = {}
    form = StarrForm(request.POST or None)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
    else:
        context['form'] = form
        return render(request, "app/create_starr.html", context)

在我的创建_明星. html

<div class="starr_cont">
<form class = "starr" method="POST" action="{% url 'app:create_starr' %}" enctype="multipart/form-data">
    {% csrf_token %}
    <p>
        <select name="user" required="" id="id_user">
            <option value="{{user.pk}}" selected="">{{user.username}}</option>
          </select>
    </p>
    <p>
        <select name="list" required="" id="id_List">
            <option value="{{list.pk}}" selected="">{{list.title}}</option>
          </select>
    </p>
    <div class="comment-stars">
        <span class="stars-post-comments">
            <span class="fa fa-star" id="st1"></span>
            <span class="fa fa-star" id="st2"></span>
            <span class="fa fa-star" id="st3"></span>
            <span class="fa fa-star" id="st4"></span>
            <span class="fa fa-star" id="st5"></span>
            </span>
    </div>
    <p>
    <input type="number" name="rate" value="0" required="" id="id_rate" placeholder="Rating">
    </p>
<div><button type="submit">Rate</button></div>
</form>
</div>
j2cgzkjk

j2cgzkjk1#

对于用户已经登录的时间,您可以在数据库中保存评级状态,如果匿名用户可以评级,您可以使用CookiesSessions,但这两种方法都不能保证防止用户多次评级

相关问题