我的网站.urls中定义的URLconf被Django尝试使用这些URL模式,按照这个顺序:

2024-05-10 13:30:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道以前有人问过这个问题,但我还没有找到解决问题的答案。

我正在查看Djangotutorial,我已经按照教程中的内容逐字设置了第一个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.

我使用的是Django 1.10.5和Python 2.7。

以下是我在相关url和视图文件中的代码:

在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.")

在mysite/polls/url.py中:

from django.conf.urls import url

from . import views

urlpatterns = [
  url(r'^%', views.index, name='index'),
]

在mysite/mysite/url.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),
]

怎么了?为什么我要打404?


Tags: thedjangoinfrompyimporthttpurl
1条回答
网友
1楼 · 发布于 2024-05-10 13:30:13

url conf regex不正确,必须使用$,而不是%

from django.conf.urls import url

from . import views

urlpatterns = [
   url(r'^$', views.index, name='index'),
]

$充当regex标志来定义正则表达式的结尾。

相关问题 更多 >