Reputation: 1254
I have declared an url like following in django 1.7:
url(r'^page(/\w{2}/|/)$', MyView.as_view(), name='my_name'),
In my template, I want to reverse the url from its name. I tried:
<form method="post" action="{% url 'my_namespace:my_name' variable %}">
<form method="post" action="{% url 'my_namespace:my_name' %}">
But nothing works, it throwed exception:
Reverse for 'my_name' with arguments '(u'test',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['page(\/\w{2}\/|\/)$']
Please help by giving me some advice. Thank you.
Upvotes: 0
Views: 410
Reputation: 1483
The regex appears incorrect for matching the argument 'test'.
If we take a look at the regex (/\w{2}/|/)
,
It appears to be capturing two groups which are separated by an 'OR' (|) operator.
The first group is /\w{2}/
. This is only going to match '/' followed by \w only 2 times followed by '/'.
The second group is only matching '/'.
'test' doesn't match either one of these cases so it raises an exception.
I believe the correct regex we are looking to capture here would be,
(\w+)
The correct URL structure would look like this,
url(r'^page/(\w+)/$', MyView.as_view(), name='my_name')
url(r'^page/$', MyView.as_view(), name='my_name')
This would match given the argument 'test' because the regex (\w+)
says match any of the characters in this group [a-zA-Z0-9_]
one or more times and each character in 'test' falls into this category.
Upvotes: 4