我知道这个问题以前曾被问过,但是我还没有找到可以解决我情况的答案。
我正在看Django 教程,并且已经按照教程中的说明逐个设置了第一个URL,但是当我转到http:// http:// localhost:8000 / polls /时,它给我这个错误:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^% [name='index']
^admin/
The current URL, polls/, didn't match any of these.
Run Code Online (Sandbox Code Playgroud)
我正在使用Django 1.10.5和Python 2.7。
这是我在相关网址和查看文件中的代码:
在mysite / polls / views.py中:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Run Code Online (Sandbox Code Playgroud)
在mysite / polls / urls.py中:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^%', views.index, name='index'),
]
Run Code Online (Sandbox Code Playgroud)
在mysite / mysite / urls.py中:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?为什么我会收到404?
您的url conf正则表达式不正确,必须使用$代替%。
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Run Code Online (Sandbox Code Playgroud)
的$作为一个正则表达式标志来定义正则表达式的结尾。