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
Related
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>', ...)
I am trying to render dynamic Url in Django template as follows
<a href={{'jibambe_site_detail/'|add: site.id}}>{{ site }}</a>. This however is returning a TemplateSyntaxError at /jibambe_sites/
add requires 2 arguments, 1 provided. What am I missing or how should I render this dynamic URL, I want it to produce something like jibambe_site_detail/1
OP's strategy changed on this one but OP's original error seems to be because of the space after colon.
Use add:site.id instead of add: site.id
See a similar question about default here
From #schwobaseggl comment, I was able to add a dynamic url variable as follows <a href={% url 'site_details' pk=site.id %}>{{ site.name }}</a>
then in the urls.py urlpatterns, I gave the path to jibambe_site_details a name path('jibambe_site_detail/<slug:pk>', JibambeSitesDetails.as_view(), name='site_details'),
I am trying to render the url of the movie clicked among numerous other movies. The url should be in the format
url/moviename. However, I am getting this error
movie_detail() got an unexpected keyword argument 'movie_name'
Please help me.... I am very new to Django, as you can see.
views code:
def movie_detail(request, movie_name):
movieneed = movies.objects.get(title = movie_name)
movieneed = movieneed.replace("_", " ")
return render(request, 'app/movie.html', {'movie':movieneed})
template code:
{% for movie in movies %}
<li data-id="{{ movie.id }}"><span class="title">
<a href="{% url "movie" movie_name=movie.movie_name %}">
{{ movie.title }}</a></span> ({{ movie.year }}) - {{ movie.genre}}<a class="delete">X</a></li>
{% endfor %}
url code:
url(r'^(?P<movie_name>)/$', views.movie_detail, name='movie'),
Ordinarily, that exception would mean that you don't have a movie_name parameter in your movie_detail function, but clearly you do.
I'm not positive, but I think the problem may be that you don't actually have anything to match in there. For example, say you only allow movie titles with letters, numbers, and hyphens (no punctuation, spaces, etc.) Then you need to use a regular expression like this:
url(r'^(?P<movie_name>[-\w]+)/$', views.movie_detail, name='movie')
I think the problem is on the url regex. read this doc, can be helpful.
url(r'^(?P<movie_name>)/$', views.movie_detail, name='movie'),
To:
url(r'^(?P<movie_name>[\w-]+)/$', views.movie_detail, name='movie'),
Named groups¶
The above example used simple, non-named regular-expression groups
(via parenthesis) to capture bits of the URL and pass them as
positional arguments to a view. In more advanced usage, it’s possible
to use named regular-expression groups to capture URL bits and pass
them as keyword arguments to a view.
In Python regular expressions, the syntax for named regular-expression
groups is (?Ppattern), where name is the name of the group and
pattern is some pattern to match.
You might want to change the movie name in the url for a slug field, slugs are specially suited fields to convert arbitrary strings into valid url strings
as for your problem, your url is not getting properly matched, try this:
url(r'^(?P<movie_name>[-\w]+)/$', views.movie_detail, name='movie'),
I have a problem to do the reverse command 'url' from the template base.html.
URLS.conf my file looks like this:
dic_info_artigo = {
'queryset': Artigo.modificado.all(),
'date_field': 'data_pub',
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^$', 'archive_index', dic_info_artigo,'artigos'),
(r'^(?P<year>\d{4})/$','archive_year', dic_info_artigo,'artigos_ano'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/$',
'archive_month', dic_info_artigo,'artigos_mes'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$',
'archive_day', dic_info_artigo,'artigos_dia'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
'object_detail', dic_info_artigo,'detalhe_artigo'),
)
base.html
<a href="{% url artigos %}"> Artigos </ a>
The error:
dictionary update sequence element # 0 has length 1; 2 is required
Already tried using the parameter 'name=', i change the value , but it did not work
url(r'^$', 'archive_index', dic_info_artigo, name='artigos'),
What am I doing wrong? Any tips?
Thanks.
The error message suggests that you have tried to name a view using something like:
(r'^my_url$', 'my_view', 'my_view')
However, the third argument should be a dictionary, not the name of a view.
To prevent errors like this, I recommend always using the url shortcut and naming the url pattern:
url(r'^my_url$', 'my_view', name='my_view')
However, you could pass an empty dictionary as the third argument if prefer:
(r'^my_url$', 'my_view', {}, 'my_view')
The urls.py you have posted looks ok, so the problem is probably in a different urls.py. If you're lucky, the full traceback might give you the exact line of the module where the error is occurring.
Use url() to name the urls and try the following in template file.
{% url 'artigos' %}
Okay I am having a bit of an issue.
I want to create a button with a link, and right now I am using action={% url views.contest_overview %} in hopes that the reverse lookup by Django will match (r'^category/$', views.contest_overview), in my urls.py. However, this is not working and I can't figure out the proper nomenclature, despite numerous guesses.
The error I get (with my best guess above) is:
Caught NoReverseMatch while rendering: Reverse for
'views.contest_overview' with arguments '()' and keyword arguments
'{}' not found.
Thank you very much for your time!
Use the application name in the url tag, e.g. {% url myapp.views.contest_overview %}
This is what I usually do; I give names to my url. For example:
url(r'^account/register/$', 'someapp.views.register_view', name='account_register'),
Therefore in template, I can do this:
{% url account_register as url_acc_register %}
<html>
..
..
Some link