In a template, I have {% url "news.views.article" article=article.id %} where article.id is the ID of an article currently being displayed. My urls.py contains this:
url(r'^news/$', 'news.views.index'),
url(r'^news/article/(?P<article>\d{1,4})/$', 'news.views.article'),
However, when I load the page containing the above templatetag, I get this:
NoReverseMatch at /news/
Reverse for '"news.views.article"' with arguments '()' and keyword arguments '{'article': 2}' not found.
Try it without the quotes around your view definition.
{% url news.views.article article=article.id %}
You've already defined the view name, so this should work by calling the function by name.
The syntax with quotes around the view name works only for Django 1.5. Since you're probably using an older version, you should remove the quotes, or add in your template:
{% load url from future %}
and then use the quotes.
More info on the deprecation - Django 1.3 release notes
Related
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.
I'm having trouble getting password_reset_confirm to work. I have looked at numerous solutions, but none seem to be working for me.
urls.py: (in particular, the third line)
(r'^resetpassword/passwordsent/$', 'django.contrib.auth.views.password_reset_done', name="reset_password"),
(r'^resetpassword/$', 'django.contrib.auth.views.password_reset', name="reset_password"),
(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)-(?P<token>,+)/$', 'django.contrib.auth.views.password_reset_confirm'),
(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
the password_reset_email.html:
{% load url from future %}
{% autoescape off %}
Someone asked for password reset for email {{ email }}. Follow the link below:
{{ protocol}}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb64=uid token=token %}
{% endautoescape %}
Everything seems to be working fine, until I submit my e-mail and get the following error:
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': 'OQ', u'token': u'3n2-0fee9d3f98dad36e63d8'}' not found. 2 pattern(s) tried: ['/$reset/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 'reset/(?P<uidb64>[0-9A-Za-z_\\-]+)-(?P<token>,+)/$']
I am using Django 1.6.
Any help is much appreciated! Thanks!
You can see from the exception what's going on, although it's a little hard to spot. If you look at what patterns it tried:
2 pattern(s) tried: ['/$reset/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 'reset/(?P<uidb64>[0-9A-Za-z_\\-]+)-(?P<token>,+)/$']
You should notice that the first pattern is the one that should generally match, in the sense that it accepts a token with a - in it. But it has a stray $ prepending the rest of its content, so actually it can't match anything:
'/$reset/...'
You don't show the urls.py line that establishes that pattern - the third line you refer to can only match a token consisting of nothing but commas:
(?P<token>,+)
So while I can safely say that you need to correct your urls.py, I cannot say exactly where you need to correct it. If you intend to match that urls.py line, you should update the token group regexp to accept your actual token values, and should figure out why the other one is around to match at all. That said, if - is a valid character to appear as part of your token I think you'll find it easier overall to use / as a divider between your uidb64 field and your token, as your first regexp does except for the stray $.
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' %}
trying to use the Django {%url %} template tag and keep failing.
the view is defined in sitename/transfers/views.py (where transfers is the django app name):
def description_ListView(requesst,**kwargs):
template_name = 'transfers/description.html'
o = get_list_or_404(Transfer, description =kwargs['description'])
#print ('o:',o)
context_object_name = "transfer_name_list"
return render_to_response(template_name,{context_object_name:o,'description':kwargs['description']})
(yes, I DO know that this code is a little strange. working on making this more generic and caught in the middle with this annoying problem)
and the url is mapped in transfers/urls.py
url(r'^description/(?P<description>[\w ]+)/$',
'transfers.views.description_ListView',
name = 'description_url')
and in the tag:
{% url "description_url" "blabla" %}
also tried:
{% url "transfers.views.Description_ListView" description = "blabla" %}
the error message:
Exception Type: NoReverseMatch
Exception Value:
Reverse for '"description_url"' with arguments '(u'blabla',)' and keyword arguments '{}' not found.
or when i Tried using the as somename syntax and calling it like this: `{{somename}}. just failed silently and didn't produce anything.
where I also tried importing the Description_ListView from the views and using it directly, didn't help.
Also, following the advice of numerous answers on this subject in various SO questions I changed to double quotes around the view, and reverted to using the url name instead of view but neither helped.
I'll be glad for any help with this
I don't think you need quotes. Try:
{% url description_url "blabla" %}
See https://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns
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