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

q8l4jmvw  于 2023-02-28  发布在  Python
关注(0)|答案(3)|浏览(187)

我从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.

下面是www.example.com文件的代码settings.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'

下面是www.example.com文件的代码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操场页面的www.example.com文件的代码

from django.urls import path
from . import views

urlpatterns= [
    path('hello/', views.say_hello)
]

下面是urls.py店面页面的www.example.com文件的代码

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

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

rsaldnfx1#

你的django项目中真的有playground文件夹和urls.py文件吗?从这里我只能看到你的应用程序名是storefronturls.py文件。所以请确保你包含了现有的url配置。
来自文档:
每当Django遇到include()时,它会切掉URL中与该点匹配的任何部分,并将剩余的字符串发送到包含的URLconf中进行进一步处理。

mcvgt66p

mcvgt66p2#

我有同样的问题,如果你或任何人正在寻找这个问题的答案,这是非常简单的,你必须在执行前保存更改,在VS代码中,我们往往忘记这样做.一旦你这样做,它的工作就像一个魅力也fyi一旦你得到它的工作,最有可能你会失去默认的主页,这是很正常的,你不必大惊小怪.

xuo3flqw

xuo3flqw3#

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

相关问题