为什么Django upvote仅增加最新添加的项目的投票?

时间:2019-03-29 14:12:33

标签: django python-3.x django-models django-forms django-views

每个项目都有一个upvote按钮,但是当我单击它时,它只会增加最新添加的项目的upvote。

<a href="javascript:{document.getElementById('upvote').submit() }">
<button  class="btn btn-success d-block w-100 mt-4"><span class="pull-left">UPVOTE <i
    class="fa fa-thumbs-o-up"></i> </span> <span class="pull-right"> {{product.upvote}} </span>
</button>
</a>
<form action="{% url 'upvote' product.id %}" method="POST" id="upvote">
    {% csrf_token %}
    <input type="hidden" name="upvote">
</form>

from django.urls import path
from myApp import views
urlpatterns = [
    path('', views.HomePageView.as_view(),name='home'),
    path('<int:product_id>/upvote',views.Upvote,name='upvote'),
]

def Upvote(request, product_id):
    if request.method == 'POST':
        product = get_object_or_404(Product, pk=product_id)
        product.upvote += 1
        product.save()
        return redirect('home')

0 个答案:

没有答案