每个项目都有一个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')