首页 > 解决方案 > Django 中的星级评定

问题描述

我看过其他关于这个的帖子,但我还是太笨了:/所以我有这个网站,里面有产品,每个产品都有其独特的页面细节,您可以在其中发表评论。现在我想在您写评论时添加“星级”产品的可能性,如图所示。我猜第一步是在“评论模型”上添加一个整数字段。但是从现在开始的步骤是什么?我猜需要一点 JS,但我不知道 js:/

模型.py

class CommentsModel(models.Model):
user = models.ForeignKey(User,on_delete=models.SET_NULL, null=True)
component = models.ForeignKey(ProductsModel,on_delete=models.SET_NULL, null=True)
text = models.TextField(null=False)
date = models.DateTimeField(default=timezone.now)
rating = models.IntegerField(default=0,
    validators = [
        MaxValueValidator(5),
        MinValueValidator(0),
    ]
)

def __str__(self):
    return '%s %s' % (self.component.name, self.user.username)

视图.py

def comments_view(request, id):
component = get_object_or_404(ProductsModel, id = id)
comments = CommentsModel.objects.filter(component = component).order_by('-id')

if request.method == 'POST':
    comment_form = CommentsForm(request.POST or None)
    if comment_form.is_valid():
        text = request.POST.get('text')
        comment_form = CommentsModel.objects.create(component=component, user=request.user, text=text)
        comment_form.save()
        return HttpResponseRedirect(component.get_absolute_url())
else:
    comment_form = CommentsForm()
    
context = {'object':component, 'object2':comments, 'comment_form':comment_form}
return render(request, 'templates/comments/comments.html', context)

这是我的网站,模板中有一些星星代表我希望的外观和工作方式

表格.py

class CommentsForm(forms.ModelForm):
class Meta:
    model = CommentsModel
    fields = [
        'text',
    ]

模板(只是带有评论的部分):

<div class="container">
    <form  method="post">
        {% csrf_token %}
        <button type="submit" class="fa fa-star fa-2x my-btn" id="first"></button>
        <button type="submit" class="fa fa-star fa-2x my-btn" id="second"></button>
        <button type="submit" class="fa fa-star fa-2x my-btn" id="third"></button>
        <button type="submit" class="fa fa-star fa-2x my-btn" id="fourth"></button>
        <button type="submit" class="fa fa-star fa-2x my-btn" id="fifth"></button>
        <div>
            {{ comment_form.text|as_crispy_field }}
        </div>
        {% if request.user.is_authenticated %}
            <input type="submit" value="Posteaza" class="btn btn-primary" style="margin-top: 20px; background-color: white; color: black; border-color: white;">
        {% else %}
            <input type="submit" value="Submit" class="btn btn-outline-succes" style="margin-top: 20px; background-color: white; color: black; border-color: white;" disabled>
        {% endif %}
    </form>
    

    <div class="main-comment-section" style="margin-top: 10px;">
        {{ object2.count }} Comentarii
        {% for index in object2 %}
            <figure style="padding-top: 10px;">
                <blockquote class="blockquote">
                    <p style="font-weight: 15px;">{{ index.text }}</p>
                </blockquote>
                <figcaption class="blockquote-footer">
                    postat de catre <cite title="Source Title">{{ index.user|capfirst}}</cite>
                </figcaption>
            </figure>
        {% endfor %}
    </div>

标签: pythondjangorating

解决方案


推荐阅读