Django:NoReverseMatch at / - >反转' name'参数'()'和关键字参数' {}'未找到。尝试了1种模式:[' $ name /']

时间:2015-04-04 06:29:20

标签: python django reverse

http://127.0.0.1:8000/我收到此错误:

NoReverseMatch at /
Reverse for 'name' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$name/']

Request Method:     GET

Request URL:    http://127.0.0.1:8000/
Django Version:     1.7.5
Exception Type:     NoReverseMatch
Exception Value:    Reverse for 'name' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$name/']

Exception Location:     /usr/local/lib/python3.4/dist-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 468

urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mydjapp.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', include('polls.urls')),
)

轮询/ urls.py

from django.conf.urls import url, patterns
from django.shortcuts import render
from polls import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^name/', views.name, name='name'),
)

views.py

from django.shortcuts import render

# Create your views here.

def index(request):
    return render(request, 'polls/index.html')

def name(request):
    return render(request, 'polls/index.html')

模板/轮询/ index.html中

<html>
    <head>
        <title>page</title>
    </head>
    <body>
        <p><a href="{% url 'name' %}">Hello</a></p>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

从包含网址中删除美元符号:

url(r'^', include('polls.urls')),