【问题标题】:How do I save user ratings in Django database?如何在 Django 数据库中保存用户评分?
【发布时间】:2019-09-27 16:23:21
【问题描述】:

这是我在 Django 中的第一个项目。我正在尝试将评分保存在 Django 数据库中,但是当我单击单选按钮时,该值不会存储在数据库中。我已经尝试过以前上传的 Stack Overflow 的解决方案,但没有一个能帮助我解决我的问题。我最初在 forms.py 中使用 RadioSelect 但仍然遇到同样的问题。

代码如下:

Model.py

class Product(models.Model):
    title = models.CharField(max_length=120)
    brand = models.CharField(max_length=120,default="None")
    model = models.CharField(max_length=120,default="None")
    slug = models.SlugField(blank=True, unique=True)
    category = models.CharField(max_length=120 , default="Phone")
    price = models.DecimalField(decimal_places=2, max_digits=20, default=39.99)

class Rating(models.Model):
    product=models.ForeignKey(Product,default=None, on_delete=models.PROTECT)
    user=models.ForeignKey(User,default=None, on_delete=models.PROTECT)
    rating = models.CharField(max_length=120)

Views.py

def add_rating(request,id):
    product = get_object_or_404(Product, pk=id)
    pro = Product.objects.get(id=id)
    if request.method == "POST":
        form = RatingForm(request.POST)
        if form.is_valid():
            product = form.cleaned_data['product']
            user = form.cleaned_data['user']
            rating = form.cleaned_data['rating']

            product = request.POST.get('product', ''),
            user = request.POST.get('user', ''),
            rating = request.POST.get('rating', ''),

            obj = Rating(product=product, user=user, rating=rating)
            obj.save()
            context = {'obj': obj}
            return render(request, 'product/detail.html',context)
        else:
           form=RatingForm()
        return HttpResponse('Please rate the product')

Forms.py

from django import forms
from .models import Rating

class RatingForm(forms.ModelForm):

   class Meta:
      model = Rating
      fields = ('product', 'user','rating')

template.py

<form method="POST" action="{% url 'add_rating' product.id %}">{%    csrf_token %}
<ul class="rate-area" style="display:inline;position:absolute">
<input type="radio" id="5-star" name="rating" value="5" /><label for="5-   star" title="Amazing">5 stars</label>
<input type="radio" id="4-star" name="rating" value="4" /><label for="4-star" title="Good">4 stars</label>
<input type="radio" id="3-star" name="rating" value="3" /><label for="3-star" title="Average">3 stars</label>
<input type="radio" id="2-star" name="rating" value="2" /><label for="2-star" title="Not Good">2 stars</label>
<input type="radio" id="1-star" name="rating" value="1" /><label for="1-star" title="Bad">1 star</label>
<button type="submit" value="Rate">Rate</button>

</ul>

</form>

【问题讨论】:

    标签: django django-models django-forms django-templates django-views


    【解决方案1】:

    您在模型上使用“CharField”,而您应该使用“ChoiceField”,ChoiceField 将成为下拉选择。

    使用通用编辑视图也会更容易; https://docs.djangoproject.com/en/2.2/ref/class-based-views/generic-editing/

    【讨论】:

    • 非常感谢@Kevin D。我已将 CharField 修改为 ChoiceField,但值未存储在数据库中。
    • 如何生成表单?您是使用 {{ form }} 生成表单还是手动循环遍历所有字段?看起来我在这里遗漏了一些项目。
    • 我没有使用 {{ form }}。我想通过遍历字段来做到这一点
    • 这就解释了为什么,开始使用 {{ form }} 来生成表单并执行测试。如果一切正常,您可以手动呈现表单,它确实需要一些工作,请检查以下docs.djangoproject.com/en/2.2/topics/forms/…
    • 非常感谢@Kevin 的宝贵时间。问题解决了。我没有在 forms.py 中使用 widget forms.RadioSelect 进行评分。
    猜你喜欢
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2021-02-22
    • 2015-06-21
    相关资源
    最近更新 更多