Django: Problem with passing parameters from a template {% url %} to a view - python

I have this urlpath:
path('download/<str:fpath>/<str:fname>', views.download, name='download'),
And this is the view:
def download(request, fpath, fname):
# some code
In template, I have this href tag and I want to pass those strings as arguments to the download view.
click me
But I get this error:
NoReverseMatch at /
Reverse for 'download' with arguments '('lms/static/lms/files/homework/Math 1/3/3', 'hmm.pdf')' not found. 1 pattern(s) tried: ['download/(?P<fpath>[^/]+)/(?P<fname>[^/]+)\\Z']
How can I fix this?

Url arguments of type str cannot contains / characters. You can see this in the error message which has translated your <str:fpath> to a regex:
tried: ['download/(?P<fpath>[^/]+)/(?P<fname>[^/]+)\\Z']
You should use a path converter for your case (see here).
For example:
path('download/<path:fpath>/<str:fname>', ...)

Related

How to send url from template to view in django

How can I send a URL from a template to a view? I have noticed that a normal string works but a URL doesn't. Here is what I have tried.
path('imageviewer/<str:theimage>', mainviews.imageviewer, name="imageviewer"),
def imageviewer(request, theimage):
response = render(request, "imageviewer.html", {"theimage": theimage})
return response
How I attempt to pass it : (value.image) is a url
<a href="{% url 'imageviewer' theimage=value.image %}" class="effect-lily tm-post-link tm-pt-60">
Error I Get:
Reverse for 'imageviewer' with keyword arguments '{'theimage': 'https://storage.googleapis.com/katakata-cb1db.appspot.com/images/humours/1643758561'}' not found. 1 pattern(s) tried: ['imageviewer/(?P<theimage>[^/]+)\\Z']
Thank you.
You need to escape the image so that it can be used in a URL, use the built-in filter urlencode
{% url 'imageviewer' theimage=value.image|urlencode %}
Your urlpattern also needs to accept slashes and other chars, use the path converter instead of str as it accepts any non-empty string
path('imageviewer/<path:theimage>', mainviews.imageviewer, name="imageviewer"),

No reversematch error eventhough pk is imported

I got a No reverseMatch Error evnthough pk has been imported.
Traceback:
Reverse for 'profile_page' with no arguments not found. 1 pattern(s) tried: ['profile_page/(?P<pro>[^/]+)$']
urls.py:
path('profile_page/<str:pro>', UserProfileView, name='profile_page'),
Reverse for 'profile_page' with no arguments not found. 1 pattern(s) tried: ['profile_page/(?P<pro>[^/]+)$']
Your Error Traceback clearly says that no arguments are found for profile_page route.
Since your url route for profile_page accepts a string, you need to pass one
urls.py
path('profile_page/<str:pro>', UserProfileView, name='profile_page'),
Wherever you are navigating to profile_page in your HTML code. you have to pass a string along with it.
I suppose you are using this route to display the profile page of a user, then use this - {% url 'profile_page' <some_user_name> %}
Eg: User

How to pass 2 arguments from template to views in django?

So I know I can pass one argument like this: {%url '...path...' argument%} but I want to pass 2 arguments like {%url '...path...' argument1 argument2%}
Here is my exact code:
search.html:
{% for id, title, thumbnail in videos%}
<img src="{{thumbnail}}">{{title}}<p style="display:inline;"> | </p>Add to playlist
<br>
{%endfor%}
urls.py:
path('select/<id>/<title>', views.select, name='select'),
I get the following error:
Reverse for 'select' with arguments '('omPiA5AsGWw', 'PRESLAVA - POSLEDEN ADRES / Преслава - Последен адрес, 2007')' not found. 1 pattern(s) tried: ['select/(?P[^/]+)/(?P[^/]+)$']
Your title contains a slash. You should specify path as path converter:
path('select/<id>/<path:title>', views.select, name='select'),
That being said, I'm not sure it is a good idea to encode such titles in the url, it will make ugly URLs with a lot of percentage encoded characters. You might want to look at slugs instead [Django-doc].
I am not sure why you want to pass two paths but you could look at using condition tags in template to avoid a lot of hardcoded templates

How do I pass parameters via url in django?

I am trying to pass a parameter to my view, but I keep getting this error:
NoReverseMatch at /pay/how
Reverse for 'pay_summary' with arguments '(False,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['pay/summary/$']
/pay/how is the current view that I'm at. (that is the current template that that view is returning).
urls.py
url(r'^pay/summary/$', views.pay_summary, name='pay_summary')
views.py
def pay_summary(req, option):
if option:
#do something
else:
#do something else
....
template
my link
EDIT
I want the view should accept a POST request, not GET.
To add to the accepted answer, in Django 2.0 the url syntax has changed:
path('<int:key_id>/', views.myview, name='myname')
Or with regular expressions:
re_path(r'^(?P<key_id>[0-9])/$', views.myview, name='myname')
You need to define a variable on the url. For example:
url(r'^pay/summary/(?P<value>\d+)/$', views.pay_summary, name='pay_summary')),
In this case you would be able to call pay/summary/0
It could be a string true/false by replacing \d+ to \s+, but you would need to interpret the string, which is not the best.
You can then use:
my link

Cannot properly generate URL with captured arguments using {% url %} template tag

I'm running into an issue with supplying captured URL parameters through template tags. I have a URL dispatch with a captured parameter that hands off to another URL dispatch with include() that does not have any captured parameters:
nodemanager.urls:
url(r'^(?P<node_id>\d+)/rank/', include('ranking.urls')),
ranking.urls:
url(r'^setup$', views.setup, name='setup'),
I am using {% url 'setup' node_id=node.id %} in my template which creates the error:
TypeError at /stage1/node/5/rank/setup
setup() got an unexpected keyword argument 'node_id'
If I take out the keyword argument and just use: {% url 'setup' %}, the landing page fails to load and I get the (predictable) error:
NoReverseMatch at /stage1/
Reverse for 'setup' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) >tried: ['stage1/node/(?P\d+)/rank/setup$']
I know that I need to supply the parameter for node_id to properly reverse the URL. However, the named url "setup" in my ranking app doesn't take any parameters, but the URL that includes it (in the nodemanager app) does.
How would I properly pass the node_id keyword argument using a template tag that points to stage1/node/5/rank/setup, i.e. something of the form {% url 'setup' ... %}. Can I do this?
Let me know if I need to post more context code; I tried to include (what I thought) are the relevant parts.
The original error is not due to the URL reversing, but comes when the view is actually called. This is probably because you have not declared it to take a node_id argument. It should be like this:
def setup(request, node_id):
...
Once you've fixed that, the original url tag syntax should work.

Categories

Resources