无法为Django评论表单实施星级评定

时间:2019-03-15 15:20:47

标签: html css django

我目前正在Django上的餐厅评论网站上工作,但是我很难在评论中实施星级评定。

这是我的Django代码:

detail.html

<form method="POST" class="post-form">
    <fieldset class="rating">
        <input type="radio" id="star5" name="rating" value="5" />
        <label class="full" for="star5" title="Awesome - 5 stars"></label>
        <input type="radio" id="star4half" name="rating" value="4half" />
        <label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
        <input type="radio" id="star4" name="rating" value="4" />
        <label class="full" for="star4" title="Pretty good - 4 stars"></label>
        <input type="radio" id="star3half" name="rating" value="3half" />
        <label class="half" for="star3half" title="Meh - 3.5 stars"></label>
        <input type="radio" id="star3" name="rating" value="3" />
        <label class="full" for="star3" title="Meh - 3 stars"></label>
        <input type="radio" id="star2half" name="rating" value="2half" />
        <label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
        <input type="radio" id="star2" name="rating" value="2" />
        <label class="full" for="star2" title="Kinda bad - 2 stars"></label>
        <input type="radio" id="star1half" name="rating" value="1half" />
        <label class="half" for="star1half" title="Meh - 1.5 stars"></label>
        <input type="radio" id="star1" name="rating" value="1" />
        <label class="full" for="star1" title="Sucks big time - 1 star"></label>
        <input type="radio" id="starhalf" name="rating" value="half" />
        <label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
    </fieldset>
    {% csrf_token %} {{ form.as_p }}

    <button type="submit" class="save btn btn-default" style="width: 100px!important; margin-left: 40%!important; border: none!important; background-color:#ffbf24!important; outline: 0!important; color: white!important;">Send</button>
</form>
{% else %}
<center>
    <h6 style="margin-left: 20px; text-align: center;">You must login to add a comment</h6></center>
</center><a href="{% url 'main_app:login' %}" class="btn btn-primary" style=" margin-left:45%!important; border: none!important; background-color:#ffbf24!important; outline: 0!important; color: white!important;">Login</a></center>
{% endif %}

</div>
</div>
</section>

views.py

@login_required
def add_review_to_restraunt(request, pk):
    restraunt = get_object_or_404(Restraunt, pk=pk)
    if request.method == "POST":
        form = ReviewForm(request.POST)
        if form.is_valid():
            print(form.cleaned_data['rating'])
            review = form.save(commit=False)
            review.restraunt = restraunt
            review.user = request.user
            review.save()
            return redirect('detail', pk=restraunt.pk)
    else:
        form = ReviewForm()
    return render(request, 'main_app/add_review_to_restraunt.html', {'form': form})

基本上,我的目标是创建一个Django表单,用户可以在其中填写餐厅的期望评分。但是当我尝试执行此操作时,我失败了,因为星级未保存或者无法在我的views.py Django文件中有效获取其值。

我尝试使用django-star-rating,但似乎无法获得我打算收到的结果,因为我只能在以前创建的数据库对象上使用该库,而不能在新创建的数据库对象上使用该库。我真的很感谢我能收到的任何反馈意见,对于我有一段时间没有发布的任何不清楚的事情表示歉意,并且很高兴澄清是否需要

0 个答案:

没有答案