Django 教程。找不到网页

Django Tutorial. Page not found

在写这个问题之前,我已经搜索了任何可能对我有帮助的答案,但我还没有找到任何有用的答案。

问题是我按照教程进行操作,但看不到我创建的视图。

现在我要分享我的代码:

项目urls.py:

from django.contrib import admin
from django.urls import include, path 

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

投票 urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path(" ", views.index, name='index'),
    #127.0.0.1/polls/
]

民意调查views.py

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
def index(request):
    HttpResponse("Welcome to de Polls Universe Index")

好的,我已经知道发生了什么:

我忘记了 de HttpResponse 之前的 RETURN。

您的代码有两个问题:

  • 索引视图的路径包含一个空白,必须将其删除
  • 视图函数必须return一个响应对象。请在最后一行前加上return