Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using {% url ??? %} in django templates

I have looked a lot on google for answers of how to use the 'url' tag in templates only to find many responses saying 'You just insert it into your template and point it at the view you want the url for'. Well no joy for me :( I have tried every permutation possible and have resorted to posting here as a last resort.

So here it is. My urls.py looks like this:

from django.conf.urls.defaults import * from login.views import * from mainapp.views import * import settings  # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover()  urlpatterns = patterns('',     # Example:     # (r'^weclaim/', include('weclaim.foo.urls')),     (r'^login/', login_view),     (r'^logout/', logout_view),     ('^$', main_view),      # Uncomment the admin/doc line below and add 'django.contrib.admindocs'      # to INSTALLED_APPS to enable admin documentation:     # (r'^admin/doc/', include('django.contrib.admindocs.urls')),      # Uncomment the next line to enable the admin:     (r'^admin/', include(admin.site.urls)),     #(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': '/home/arthur/Software/django/weclaim/templates/static'}),     (r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), ) 

My 'views.py' in my 'login' directory looks like:

from django.shortcuts import render_to_response, redirect from django.template import RequestContext from django.contrib import auth  def login_view(request):     if request.method == 'POST':         uname = request.POST.get('username', '')         psword = request.POST.get('password', '')         user = auth.authenticate(username=uname, password=psword)         # if the user logs in and is active         if user is not None and user.is_active:             auth.login(request, user)             return render_to_response('main/main.html', {}, context_instance=RequestContext(request))             #return redirect(main_view)         else:             return render_to_response('loginpage.html', {'box_width': '402', 'login_failed': '1',}, context_instance=RequestContext(request))     else:         return render_to_response('loginpage.html', {'box_width': '400',}, context_instance=RequestContext(request))  def logout_view(request):     auth.logout(request)     return render_to_response('loginpage.html', {'box_width': '402', 'logged_out': '1',}, context_instance=RequestContext(request)) 

and finally the main.html to which the login_view points looks like:

<html> <body> test! <a href="{% url logout_view %}">logout</a> </body> </html> 

So why do I get 'NoReverseMatch' every time?

*(on a slightly different note I had to use 'context_instance=RequestContext(request)' at the end of all my render-to-response's because otherwise it would not recognise {{ MEDIA_URL }} in my templates and I couldn't reference any css or js files. I'm not to sure why this is. Doesn't seem right to me)*

like image 309
Robert Johnstone Avatar asked Jan 04 '11 23:01

Robert Johnstone


People also ask

What does form {% URL %} do?

{% url 'contact-form' %} is a way to add a link to another one of your pages in the template. url tells the template to look in the URLs.py file. The thing in the quotes to the right, in this case contact-form , tells the template to look for something with name=contact-form .

What does {% include %} does Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.


2 Answers

The selected answer is out of date and no others worked for me (Django 1.6 and [apparantly] no registered namespace.)

For Django 1.5 and later (from the docs)

Warning Don’t forget to put quotes around the function path or pattern name!

With a named URL you could do:

(r'^login/', login_view, name='login'), ... <a href="{% url 'login' %}">logout</a> 

Just as easy if the view takes another parameter

def login(request, extra_param): ... <a href="{% url 'login' 'some_string_containing_relevant_data' %}">login</a> 
like image 167
Mike S Avatar answered Sep 21 '22 12:09

Mike S


Instead of importing the logout_view function, you should provide a string in your urls.py file:

So not (r'^login/', login_view),

but (r'^login/', 'login.views.login_view'),

That is the standard way of doing things. Then you can access the URL in your templates using:

{% url login.views.login_view %} 
like image 38
Marcus Whybrow Avatar answered Sep 25 '22 12:09

Marcus Whybrow