首页 > 解决方案 > 找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:8000/hello

问题描述

Django 版本 = 1.11
Python 版本 = 3.6.5
kalani 是项目文件夹
testapp 是应用程序文件夹

我认为我的问题出在 urls.py 中。

http://127.0.0.1:8000/你好

错误信息:

 ERROR:

 Page not found (404)
 Request Method:    GET
 Request URL:   http://127.0.0.1:8000/hello
 Using the URLconf defined in kalani.urls, Django tried these URL 
 patterns, in this order:

^admin/
The current path, hello, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django 
settings file. Change that to False, and Django will display a standard 
404 page

测试应用程序/views.py:

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def hello(request):
   return HttpResponse('<h1>Hello world</h1>')

卡拉尼/urls.py:

 from django.conf.urls import url
 from django.contrib import admin
 from testapp import views

 urlpatterns = [
     url(r'^admin/', admin.site.urls),
     url(r'^hello/', views.hello)
 ]

卡拉尼/settings.py

INSTALLED_APPS = [
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'testapp'
]

标签: pythondjango

解决方案


您必须将这些 url 导入到ROOT_URLCONF您下的变量settings.py中,例如:

...
ROOT_URLCONF = 'kalani.urls'
...

推荐阅读