Reputation: 159
In Product model and i want to query by food title. and return me NoReverseMatch
error
html url:
<a href="{% url 'product' food_name=catalog.food_name|urlencode %}">some text</a>
views.py:
def product(request, food_name):
product = Catalog.objects.get(food_name=food_name)
return render(request, 'food/product.html', {'product':product})
url.py
url(r'^product/(?P<food_name>\w+)/', food_views.product, name='product'),
Trace
NoReverseMatch: Reverse for 'product' with arguments '()' and keyword arguments '{u'food_name': u'%D9%86%D8%A7%D9%86%20%D8%A8%D9%88%D8%B1%DA%A9'}' not found. 1 pattern(s) tried: [u'product/(?P<food_name>\\w+)/']
Upvotes: 2
Views: 64
Reputation: 43320
Remove the urlencode
, you don't need it
<a href="{% url 'product' food_name=catalog.food_name %}">some text</a>
urlencode
is used when you need to encode a string in a way that will allow it to be used inside a url (such as when you're adding get parameters). Above, you are just passing a string parameter to a function that is constructing a url.
You seem to be trying to encode arabic characters into your url which are not matched by \w
so you need to update your url to support these
^product/(?P<food_name>[\w\u0600-\u06FF]+)/
Will handle most of these (See this regexr example), but I'm not familiar with arabic enough to know what the unicode for ک
is
Upvotes: 3
Reputation: 5641
I believe it's because \w+
doesn't match URL-encoded string. Try to change it temporarily to .*
(just to check if there are not any other issues). If it will work — change \w+
to better template matching URL-encoded strings.
Upvotes: 1