use of url in django - python

I have a doubt in use of urls and path in urls.py.
I have tried a url in django as:
url('budget-info/<str:username>',views.app_view, name='app') along with:
return HttpResponseRedirect(reverse('app', kwargs={'username':user.username})) and
return HttpResponseRedirect(reverse('app', args=(user.username,)))
in my views.py. I am getting error of this type:
Reverse for 'app' with keyword arguments '{'username': 'admin'}' not found. 1 pattern(s) tried: ['budget-info/<str:username>']
However this is working fine with path in replace of url. I cannot understand why this is happening considering that both url and path are used to design url.

If you read this documentation on the URL dispatcher, nowhere url is mentioned. So please follow the docs and use path or re_path only.
url is deprecated. You can find it in books and tutorials that were made for Django versions 1.11 and earlier. It works differently than path (it's actually equivalent to re_path) as it always requires a regular expression to match variables.

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})

URL pattern for query string parameters Django REST Framework

This is my current url path is as http://localhost:8000/api/projects/abcml/2021
and in the urls.py page I am passing as path("api/projects/<str:project_handle>/<int:year>", functionname...) and in the view, I accept this parameters with self.kwargs.get method.
I want to pass url as this format http://localhost:8000/api/projects/abcml/?year=2021
What changes do I need to make in url pattern?
I tried this path("api/projects/<str:project_handle>/?year=<int:year> but did not seem correct also in the view page, instead of self.kwargs.get I changed it to self.request.query_params.get for year parameter. That did not work either. Error it throwing is Page not found (404).
The query string [wiki] is not part of the path, and therefore can not be matched.
You thus specify as path:
path('api/projects/<str:project_handle>/', functionname)
In the view, you can access the data with self.request.GET['year'] and this will return a string or a KeyError in cas the year was not provided in the query string.

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})

What is .<format> argument for?

Django==1.11
django-extensions==1.9.7
/api/userprofiles/<pk>/ poinkbackend.apps.userprofiles.api.viewsets.UserProfileViewset api:userprofile-detail
/api/userprofiles/<pk>\.<format>/ poinkbackend.apps.userprofiles.api.viewsets.UserProfileViewset api:userprofile-detail
/a
Docs say
show_urls - Displays the url routes that are defined in your project. Very crude at this point.
I had searched with format keyword. But the results are not related to my question.
Does it stand for ?format=json argument?
I had tried replacing json by html, xml. I got error in return.
Reference:
http://django-extensions.readthedocs.io/en/latest/command_extensions.html?highlight=show_urls
In that URL pattern, format is a keyword argument in the URL pattern (unlike ?format=json where it's in the querystring). A matching URL would be:
/api/userprofiles/5.json/

Python Django: urls.py questions

Need a little help with my urls.py and other stuff.
How can I replicate this in Django?
1) When user requests a non-existent page it will redirect to one up the directory level. Ex: example.com/somegoodpage/somebadpage should be redirected to example.com/somegoodpage.
2) When user requests page example.com/foo/bar/?name=John it will make url to example.com/foo/bar/name=John
3) When user requests page example.com/foo/bar/John it will change url to example.com/foo/bar/name=John.
Any help is greatly appreciated. Thank You.
For 1), if you don't want to do a separate route for every single route on your website, you'll need middleware that implements process_exception and outputs an HttpResponseRedirect.
For 2 and 3, those are rules that are presumably limited to specific routes, so you can do them without middleware.
2 might be doable in urls.py with a RedirectView, but since the relevant bit is a query string argument, I would probably make that an actual view function that looks at the query string. Putting a ? character in a url regex seems strange because it will interfere with any other use of query strings on that endpoint, among other reasons.
For 3, that's a straightforward RedirectView and you can do it entirely in urls.py.
according to django doc for number 1:
django URL dispatcher runs through each URL pattern, in order, and stops at the first one that matches the requested URL, so add a pattern that matches "somebadpage"s and assign it to a view which redirects the user to "somegoodpage".
for number 2:
the doc says "The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name."
so i don't think that you can get the "?name=John" in url dispather, so if you describe what you want to do maybe I can help better
and for 3:
to capture bits of the URL and pass them as positional arguments to a view you should use named regular-expression groups, for example :
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', 'news.views.month_archive'),
and the request to /articles/2005/03/ would call the function news.views.month_archive(request, year='2005', month='03'), instead of news.views.month_archive(request, '2005', '03').
hope this helped :)

Categories