I have error in Django: "django.urls.exceptions.NoReverseMatch: Reverse for ‘save-review’ not found. ‘save-review’ is not a valid view function or pattern name."
I study django through YouTube, I do everything as in the video, but I get an error. When there is no authorization and "Add review" button is hidden, the page works fine. But when "Add review" button is shown, the product page doesn’t load and throws an error.
product-detail.html
<div class="col-md-6">
<h3 class="my-3">Обзоры - 4.5/5 <i class="fa fa-star text-warning"></i>
{% if user.is_authenticated %}
<button type="button" data-toggle="modal" data-target="#productReview"
class="btn btn-warning bt-sm float-right">Add review</button>
{% endif %}
</h3>
<div class="card">
<div class="card-body" style="max-height: 400px; overflow; auto;">
<!-- Detail -->
<blockquote class="blockquote text-right">
<small>Good product</small>
<footer class="blockquote-footer">John Lenon
<cite title="Source Title">
<i class="fa fa-star text-warning"></i>
<i class="fa fa-star text-warning"></i>
<i class="fa fa-star text-warning"></i>
<i class="fa fa-star text-warning"></i>
</cite>
</footer>
</blockquote>
</div>
</div>
</div>
</div>
{% if user.is_authenticated %}
<!-- Product Review -->
<div class="modal fade" id="productReview" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Product Review</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="addForm" method="post" id="addForm" action="{% url 'save-review' data.id %}">
{% csrf_token %}
<table class="table table-bordered">
{{form.as_table}}
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary">Submit</button>
<input type="reset" value="Reset" id="reset" />
</td>
</tr>
</table>
<p class="ajaxRes"></p>
</form>
</div>
</div>
</div>
</div>
<!-- Product Review End -->
{% endif %}
urls.py
from django.urls import path, include
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns=[
path('', views.home, name='home'),
path('product-list', views.product_list, name='product-list'),
path('checkout', views.checkout, name='checkout'),
path('save-review/<int:pid>', views.save_review, name='save_review'),
]
views.py
def save_review(request, pid):
product = Product.objects.get(pk=pid)
user = request.user
review = ProductReview.objects.create(
user=user,
product=product,
review_text=request.POST['review_text'],
review_rating=request.POST['review_rating'],
)
return JsonResponse({'bool': True})
custom.js
$("#addForm").submit(function(e){
$.ajax({
data:$(this).serialize(),
method:$(this).attr('method'),
url:$(this).attr('action'),
dataType:'json',
success:function(res){
if(res.bool==true){
$(".ajaxRes").html('Data has been added.');
$("#reset").trigger('click');
}
}
});
e.preventDefault();
When I insert action="#"
instead of action="{% url 'save-review' data.id %
}", the error disappears. Tried changing path
in url.py
, didn’t help either. Tried solutions from SO. Cleared cache, rebooted PC. I understand that python doesn’t understand what to do with ‘save-review’, but why? The video has been on YouTube for a year, maybe something has changed in the syntax?
>Solution :
Your URL is named save_review
, but you have written {% url 'save-review' data.id %}
in your HTML.
Replace by {% url 'save_review' data.id %}
for making your code work.