python - 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>
解决方案
推荐阅读
- html - 在没有 javascript 或请求参数的情况下在重定向之间保持 http 表单值
- vue.js - Vue/Nuxt 中的 DFP 广告效果不佳
- jquery - 错误不要在下拉菜单中打开链接
- r - 我没有得到方差分析 lsmeans 的显着差异,但有一些非常显着的数据。我的脚本有什么问题?
- insert - SQL Query 将选择查询的转置插入另一个表
- mms - 支持 MMS 和 GOOSE 驱动程序的 ScadaLTS 功能
- java - 在微调器中显示 TTS 可用语言
- python - 如何确定当前的 Python egg 缓存目录?
- bash - 为什么在 USERNAME 设置为其他内容时服务以 root 身份运行
- java - Punch Clock While 循环故障