product upvote

This commit is contained in:
Sharad Ahlawat 2020-03-10 14:28:13 -07:00
parent b46e94fda0
commit f79c29b7e0
3 changed files with 17 additions and 1 deletions

View File

@ -19,7 +19,7 @@
<img src="{{ product.image.url }}" class="img-fluid" \> <img src="{{ product.image.url }}" class="img-fluid" \>
</div> </div>
<div class="col-4"> <div class="col-4">
<a href=""><button class="btn btn-primary btn-lg btn-block"><span class="oi oi-caret-top"></span> Up Vote : {{ product.votes_total }}</button></a> <a href="javascript:{document.getElementById('upvote').submit()}"><button class="btn btn-primary btn-lg btn-block"><span class="oi oi-caret-top"></span> Up Vote : {{ product.votes_total }}</button></a>
</div> </div>
</div> </div>
<hr /> <hr />
@ -37,4 +37,10 @@
<p>{{ product.body }}</p> <p>{{ product.body }}</p>
</div> </div>
</div> </div>
<form id="upvote" method="POST" action="{% url 'upvote' product.id %}">
{% csrf_token %}
<input type="hidden">
</form>
{% endblock %} {% endblock %}

View File

@ -4,4 +4,5 @@ from . import views
urlpatterns = [ urlpatterns = [
path('create', views.create, name='create'), path('create', views.create, name='create'),
path('<int:product_id>/', views.detail, name='detail'), path('<int:product_id>/', views.detail, name='detail'),
path('<int:product_id>/upvote', views.upvote, name='upvote'),
] ]

View File

@ -35,3 +35,12 @@ def create(request):
def detail(request, product_id): def detail(request, product_id):
product = get_object_or_404(Product, pk=product_id) product = get_object_or_404(Product, pk=product_id)
return render(request, 'products/detail.html', {'product': product}) return render(request, 'products/detail.html', {'product': product})
@login_required()
def upvote(request, product_id):
if request.method == 'POST':
product = get_object_or_404(Product, pk=product_id)
product.votes_total += 1
product.save()
return redirect('/products/' + str(product.id))