当我使用django加载页面时出现问题,找不到页面(404)请求方法:GET



我遵循了YouTube上关于django的教程,但当我创建一个页面并尝试加载它时,我遇到了一个问题。服务器似乎运行正常,但当加载我创建的页面时,我收到了以下错误:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/playground/hello
Using the URLconf defined in storefront.urls, Django tried these URL patterns, in this order:
admin/
The current path, playground/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.

这是设置的代码.py文件

from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-_ko=j(tbb+7&m^e-_ti8upt9d6nt#bxl_4&2*tk-co5s!gph+h'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',

'django.contrib.messages',
'django.contrib.staticfiles',
'playground'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'storefront.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'storefront.wsgi.application'

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}

# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

这是views.py文件的代码

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def say_hello(request):
return HttpResponse('Hello World')

这是操场页面的urls.py文件的代码

from django.urls import path
from . import views
urlpatterns= [
path('hello/', views.say_hello)
]

这是店面页面的urls.py文件的代码

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('playground/', include('playground.urls'))
]

您的django项目中是否真的有playground文件夹和urls.py文件?从这里我只能看到你的应用程序名称是storefronturls.py文件。因此,请确保包含现有的url配置。

来自文档:

每当Django遇到include((时,它就会截断与该点匹配的URL的任何部分,并将剩余的字符串发送到包含的URLconf中进行进一步处理。

我也遇到了同样的问题,如果你或任何人正在寻找这个问题的答案,很简单,你必须在执行之前保存更改,在VS代码中,我们往往会忘记这样做。一旦你这样做了,它就像一个符咒一样工作。一旦你让它工作起来,你很可能会丢失默认主页,这很正常,你不必担心。

im也关注youtube totarial,我遇到了同样的问题,原因是我保存了所有文件后没有保存所有文件(ctrl+s(,然后刷新了浏览器,它正常工作

相关内容

  • 没有找到相关文章

最新更新