如何在django中使用url标签?

时间:2017-06-20 19:40:08

标签: python django

我正在使用Django版本1.10.7。目前在我的HTML中我有:

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

我的项目urls.py有

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'),
]

在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',{})

这给出了错误:

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: []

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

尝试按此顺序放置您的网址:

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'),
]

如果有效,则表示您的其他已包含的网址文件存在问题。