Lawrence Pang
Lawrence Pang

Reputation: 227

How to use url tag in django?

I am using Django version 1.10.7. Currently in my html I have:

<a href={% url 'home' %}>Home<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-home"></span></a>

My project urls.py has

from django.conf.urls import include,url
from django.contrib import admin
from base import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^team/', include('team.urls'), name='team'),
    url(r'^game/', include('game.urls'), name='game'),
    url(r'^about/', include('base.urls'), name='about'),
    url(r'^$', views.home, name='home'),
]

And in views.py,

from django.shortcuts import render

# Create your views here.
def about(request):
    return render(request,'base/about.html',{})

def home(request):
    return render(request,'base/home.html',{})

This gives the error:

NoReverseMatch at /
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.10.7
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

How can I fix this?

Upvotes: 0

Views: 481

Answers (1)

Juan Diego Garcia
Juan Diego Garcia

Reputation: 823

Try to put your urls in this order:

urlpatterns = [
   url(r'^admin/', admin.site.urls),
   url(r'^$', views.home, name='home'),
   url(r'^team/', include('team.urls'), name='team'),
   url(r'^game/', include('game.urls'), name='game'),
   url(r'^about/', include('base.urls'), name='about'),
]

if works, it means that there is a problem in your other included urls files.

Upvotes: 1

Related Questions