django url no reverse match - python

I am trying to pass two parameters through the url in django (email,name) it works fine when I am just passing the email through but when I pass the name through as well I get an error (no reverse match) my name contains a space
url pattern
url(r'^details/(?P<email>[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/(?P<name>([\W ]+))/$', details, name="details")
full error message
Reverse for 'password' with arguments '(u'tom#example.com', u'tom')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['details/(?P<email>[\\w.%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4})/(?P<name>([\\W ]+))/$
passing variable through to the url
{%url 'details' email name %}

You probably want to change
(?P<name>(\W+)) # non-alphanumeric
to
(?P<name>\w+) # alphanumeric
The upper-case version is, one would assume, the exact opposite of what you want to allow for names (and urls in general).
Python regex syntax docs.

What is the exact error you are getting. When working with multiple variable you can try the following.
{%url 'details' email=email name=name %}
Update:
url(r'^details/(?P<email>[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/(?P<name>([\W ]+))/$', details, name="details")

Related

django NoReverseMatch not a valid view function or pattern name [duplicate]

I have some code and when it executes, it throws a NoReverseMatch, saying:
NoReverseMatch at /my_url/ Reverse for 'my_url_name' with arguments '()' and keyword arguments '{}' not found. n pattern(s) tried: []
What does this mean, and what can I do about it?
The NoReverseMatch error is saying that Django cannot find a matching url pattern for the url you've provided in any of your installed app's urls.
The NoReverseMatch exception is raised by django.core.urlresolvers when a matching URL in your URLconf cannot be identified based on the parameters supplied.
To start debugging it, you need to start by disecting the error message given to you.
NoReverseMatch at /my_url/
This is the url that is currently being rendered, it is this url that your application is currently trying to access but it contains a url that cannot be matched
Reverse for 'my_url_name'
This is the name of the url that it cannot find
with arguments '()' and
These are the non-keyword arguments its providing to the url
keyword arguments '{}' not found.
These are the keyword arguments its providing to the url
n pattern(s) tried: []
These are the patterns that it was able to find in your urls.py files that it tried to match against
Start by locating the code in your source relevant to the url that is currently being rendered - the url, the view, and any templates involved. In most cases, this will be the part of the code you're currently developing.
Once you've done this, read through the code in the order that django would be following until you reach the line of code that is trying to construct a url for your my_url_name. Again, this is probably in a place you've recently changed.
Now that you've discovered where the error is occuring, use the other parts of the error message to work out the issue.
The url name
Are there any typos?
Have you provided the url you're trying to access the given name?
If you have set app_name in the app's urls.py (e.g. app_name = 'my_app') or if you included the app with a namespace (e.g. include('myapp.urls', namespace='myapp'), then you need to include the namespace when reversing, e.g. {% url 'myapp:my_url_name' %} or reverse('myapp:my_url_name').
Arguments and Keyword Arguments
The arguments and keyword arguments are used to match against any capture groups that are present within the given url which can be identified by the surrounding () brackets in the url pattern.
Assuming the url you're matching requires additional arguments, take a look in the error message and first take a look if the value for the given arguments look to be correct.
If they aren't correct:
The value is missing or an empty string
This generally means that the value you're passing in doesn't contain the value you expect it to be. Take a look where you assign the value for it, set breakpoints, and you'll need to figure out why this value doesn't get passed through correctly.
The keyword argument has a typo
Correct this either in the url pattern, or in the url you're constructing.
If they are correct:
Debug the regex
You can use a website such as regexr to quickly test whether your pattern matches the url you think you're creating, Copy the url pattern into the regex field at the top, and then use the text area to include any urls that you think it should match against.
Common Mistakes:
Matching against the . wild card character or any other regex characters
Remember to escape the specific characters with a \ prefix
Only matching against lower/upper case characters
Try using either a-Z or \w instead of a-z or A-Z
Check that pattern you're matching is included within the patterns tried
If it isn't here then its possible that you have forgotten to include your app within the INSTALLED_APPS setting (or the ordering of the apps within INSTALLED_APPS may need looking at)
Django Version
In Django 1.10, the ability to reverse a url by its python path was removed. The named path should be used instead.
If you're still unable to track down the problem, then feel free to ask a new question that includes what you've tried, what you've researched (You can link to this question), and then include the relevant code to the issue - the url that you're matching, any relevant url patterns, the part of the error message that shows what django tried to match, and possibly the INSTALLED_APPS setting if applicable.
A very common error is when you get with arguments ('',). This is caused by something like this:
{% url 'view-name' does_not_exist %}
As does_not_exist doesn't exist, django evaluates it to the empty string, causing this error message.
If you install django-fastdev you will instead get a nice crash saying does_not_exist doesn't exist which is the real problem.
With django-extensions you can make sure your route in the list of routes:
./manage.py show_urls | grep path_or_name
If the route is missing you probably have not imported the application.
It may be that it's not loading the template you expect. I added a new class that inherited from UpdateView - I thought it would automatically pick the template from what I named my class, but it actually loaded it based on the model property on the class, which resulted in another (wrong) template being loaded. Once I explicitly set template_name for the new class, it worked fine.
The arguments part is typically an object from your models. Remember to add it to your context in the view. Otherwise a reference to the object in the template will be empty and therefore not match a url with an object_id.
Watch out for different arguments passing between reverse() and redirect() for example:
url(r"^some_app/(?P<some_id>\d+)/$", some_view_function, name="some_view")
will work with:
reverse("some_view", kwargs={"some_id": my_id})
and:
redirect("some_view", some_id=my_id)
but not with:
reverse("some_view", some_id=my_id)
and:
redirect("some_view", kwargs={"some_id": my_id})

How to fix NoReverseMatch error in Django [duplicate]

I have some code and when it executes, it throws a NoReverseMatch, saying:
NoReverseMatch at /my_url/ Reverse for 'my_url_name' with arguments '()' and keyword arguments '{}' not found. n pattern(s) tried: []
What does this mean, and what can I do about it?
The NoReverseMatch error is saying that Django cannot find a matching url pattern for the url you've provided in any of your installed app's urls.
The NoReverseMatch exception is raised by django.core.urlresolvers when a matching URL in your URLconf cannot be identified based on the parameters supplied.
To start debugging it, you need to start by disecting the error message given to you.
NoReverseMatch at /my_url/
This is the url that is currently being rendered, it is this url that your application is currently trying to access but it contains a url that cannot be matched
Reverse for 'my_url_name'
This is the name of the url that it cannot find
with arguments '()' and
These are the non-keyword arguments its providing to the url
keyword arguments '{}' not found.
These are the keyword arguments its providing to the url
n pattern(s) tried: []
These are the patterns that it was able to find in your urls.py files that it tried to match against
Start by locating the code in your source relevant to the url that is currently being rendered - the url, the view, and any templates involved. In most cases, this will be the part of the code you're currently developing.
Once you've done this, read through the code in the order that django would be following until you reach the line of code that is trying to construct a url for your my_url_name. Again, this is probably in a place you've recently changed.
Now that you've discovered where the error is occuring, use the other parts of the error message to work out the issue.
The url name
Are there any typos?
Have you provided the url you're trying to access the given name?
If you have set app_name in the app's urls.py (e.g. app_name = 'my_app') or if you included the app with a namespace (e.g. include('myapp.urls', namespace='myapp'), then you need to include the namespace when reversing, e.g. {% url 'myapp:my_url_name' %} or reverse('myapp:my_url_name').
Arguments and Keyword Arguments
The arguments and keyword arguments are used to match against any capture groups that are present within the given url which can be identified by the surrounding () brackets in the url pattern.
Assuming the url you're matching requires additional arguments, take a look in the error message and first take a look if the value for the given arguments look to be correct.
If they aren't correct:
The value is missing or an empty string
This generally means that the value you're passing in doesn't contain the value you expect it to be. Take a look where you assign the value for it, set breakpoints, and you'll need to figure out why this value doesn't get passed through correctly.
The keyword argument has a typo
Correct this either in the url pattern, or in the url you're constructing.
If they are correct:
Debug the regex
You can use a website such as regexr to quickly test whether your pattern matches the url you think you're creating, Copy the url pattern into the regex field at the top, and then use the text area to include any urls that you think it should match against.
Common Mistakes:
Matching against the . wild card character or any other regex characters
Remember to escape the specific characters with a \ prefix
Only matching against lower/upper case characters
Try using either a-Z or \w instead of a-z or A-Z
Check that pattern you're matching is included within the patterns tried
If it isn't here then its possible that you have forgotten to include your app within the INSTALLED_APPS setting (or the ordering of the apps within INSTALLED_APPS may need looking at)
Django Version
In Django 1.10, the ability to reverse a url by its python path was removed. The named path should be used instead.
If you're still unable to track down the problem, then feel free to ask a new question that includes what you've tried, what you've researched (You can link to this question), and then include the relevant code to the issue - the url that you're matching, any relevant url patterns, the part of the error message that shows what django tried to match, and possibly the INSTALLED_APPS setting if applicable.
A very common error is when you get with arguments ('',). This is caused by something like this:
{% url 'view-name' does_not_exist %}
As does_not_exist doesn't exist, django evaluates it to the empty string, causing this error message.
If you install django-fastdev you will instead get a nice crash saying does_not_exist doesn't exist which is the real problem.
With django-extensions you can make sure your route in the list of routes:
./manage.py show_urls | grep path_or_name
If the route is missing you probably have not imported the application.
It may be that it's not loading the template you expect. I added a new class that inherited from UpdateView - I thought it would automatically pick the template from what I named my class, but it actually loaded it based on the model property on the class, which resulted in another (wrong) template being loaded. Once I explicitly set template_name for the new class, it worked fine.
The arguments part is typically an object from your models. Remember to add it to your context in the view. Otherwise a reference to the object in the template will be empty and therefore not match a url with an object_id.
Watch out for different arguments passing between reverse() and redirect() for example:
url(r"^some_app/(?P<some_id>\d+)/$", some_view_function, name="some_view")
will work with:
reverse("some_view", kwargs={"some_id": my_id})
and:
redirect("some_view", some_id=my_id)
but not with:
reverse("some_view", some_id=my_id)
and:
redirect("some_view", kwargs={"some_id": my_id})

pass hypen in django url

I'm trying to include all characters including hyphens after /blog/* and pass them to my function in views.py.
This is my URL
/blog/entry-id-01
I have this in my urls.py
urls(r'^blog/(?P<entry>[-\w]+)/$, blog_entry)
However, when I load my page. I get the the following error:
TemplateDoesNotExist at /blog/entry-id-01
My exception value is:
blog_pages/articles/entry.html
Is there a way to include all characters including hyphens?
edit: added view.py
def blog_entry (request, entry):
return render( request, 'blog_pages/articles/'+entry+'.html')
I fixed my problem by using this regex
(?P<entry>[^/]+)
Regex for matching word characters should be \w not /w and also you need to specify the name for the named capturing group.
url(r'^blog/(?P<id>[-\w]+)/$', blog_entry)
^^ ^
As noted in the comments, your error has nothing to do with the regex, it would return a NoReverseMatch if that were the case. You can easily prove this by either pointing the url to a different view that does work, or by changing the template file that the view returns to a template that works.
I'd imagine you really dont have a different html template file for every single possible entry
'blog_pages/articles/entry-n.html'
and its more likely that you have one single html template that all entries should use so you should reference that
'blog_pages/articles/entry.html'

Django: Search URL for keyword RegExp

I have the following URL:
http://127.0.0.1:8000/databaseadd/q=DQ-TDOXRTA?recipe=&recipe=&recipe=&recipe=&recipe=
I'd like to match if the keyword "recipe=" is found in the url.
I have the following line in django url.py but it's not working:
url(r'recipe=', 'plots.views.add_recipe'),
Are the "&" and the "?" throwing it off?
Thanks!
Alex
Part after the ? not used in the url dispatching process because it contains GET parameters. Otherwise you will not be able to use a GET request with parameters on patterns like r'^foo/&' (ampersand at the end means the end of the string, and without it it would be harder to use patterns like r'^foo/' and r'^foo/bar/' at the same time).
So for your url the pattern should look like r'^databaseadd/q=DQ-TDOXRTA&' and then in add_recipe view you need to check for the recipe GET parameter.
I'd like to match if the keyword "recipe=" is found in the url
You could try something like :
import re;
re.match (r'.*recipe=.*', "http://127.0.0.1:8000/databaseadd/q=DQ-TDOXRTA?recipe=&recipe=&recipe=&recipe=&recipe=")

How to pass argument to url dispatcher from url template tag

Consider the following url pattern:
url(r'^view/(?P<id>\d+/$)', login_required(views.ViewUser.as_view()), name='user_view')
Now, in a template I want to use the url template tag to obtain the reversed path to a particular user. I was trying to accomplish this by doing the following:
However, this is throwing the following error:
Reverse for 'user_view' with arguments '()' and keyword arguments '{u'id': 2}' not found.
So, obviously the argument is getting put into a kwargs dictionary successfully, but evidently that can't be matched by the urlpattern. So, how do I fix this elegantly?
You have small mistake in regex string:
r'^view/(?P<id>\d+)/$'

Categories