运行服务器后出现Django 404错误(“找不到页面”)

时间:2018-07-09 10:28:02

标签: python django

我的项目名为“页面”,当我运行服务器时,出现此错误。任何人都有解决此错误的线索。

  

找不到页面(404)   请求方法:GET请求URL:http://127.0.0.1:8000/使用   在pages_project.urls中定义的URLconf,Django尝试了这些URL   模式,顺序如下:admin /空路径与以下任何一个都不匹配   这些。您看到此错误的原因是您的DEBUG = True   Django设置文件。将其更改为False,Django将显示一个   标准404页面。

我的settings.py文件如下:

"""
Django settings for pages_project project.

Generated by 'django-admin startproject' using Django 2.0.7.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*u)%-zmf=2g-c*3z2-&=n=asty5v36n62^90_u*-#83!wb=)eq'

# 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.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pages', # new
]

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 = 'pages_project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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 = 'pages_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

“页面”文件夹中的urls.py是

from django.urls import path


from . import views


urlpatterns = [

    path(' ', views.HomePageView.as_view(), name='home'),

]

任何帮助将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:2)

该错误告诉您主urls.py仅包含管理模式。您还需要添加页面网址。

此外,您的路径模式中还有一个空格。删除它。

答案 1 :(得分:0)

您的路径中有一个空格' '。这将意味着该URL为http://127.0.0.1:8000/%20。删除空格。

path('', views.HomePageView.as_view(), name='home')

答案 2 :(得分:0)

您投影网址:删除空格

pages / urls.py

urlpatterns = [
    path(r'^$', views.HomePageView.as_view(), name='home'),
]

在您的项目网址中添加您的应用程序网址,以便您的项目可以看到您的应用程序网址

projectName / urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.views.static import serve

urlpatterns = [     
    ...  
    url(r'^', include('pages.urls')),      
]