gpt4 book ai didi

python - Django 不呈现第二个应用程序 HTML

转载 作者:行者123 更新时间:2023-11-28 18:32:55 26 4
gpt4 key购买 nike

我添加了第二个应用程序:

python manage.py startapp about

在我的项目中。但是现在它只呈现我的第一个应用程序 View ,无论我输入什么 URL。

例。 http://127.0.0.1:8000/aldsjfal/asdfadsfa/adsfasdf/adfadsf/

这是我的设置.py:

"""
Django settings for mysite project.

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

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

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

import os
################################################################################
# Dependencies #
################################################################################
import os.path #
# #
# Note that we use os.path to construct the absolute path. This ensures Django#
# can locate the files unambiguously for STATICFILES_DIRS. - LT 16JAN2016 #
# #
# #
# #
################################################################################

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*ih=!lu+z1=5sk8&ool2hmryc07rbuhjwy*3745=i6@$w)joo7'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']

Temp_Path = os.path.realpath('.')

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

MEDIA_ROOT = PROJECT_PATH + '/media/'


# Application definition

INSTALLED_APPS = [

'about.apps.AboutConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home.apps.HomeConfig',
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.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 = 'mysite.wsgi.application'


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

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


# Password validation
# https://docs.djangoproject.com/en/1.9/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/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Los_Angeles'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'


STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

--------------------------------编辑1------------ ------------------------

这是我的根项目中的 urls.py:

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Import the include() function: from django.conf.urls import url, include
3. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = [
url(r'^about/', include('about.urls'), name ='about'),
url(r'^', include('home.urls'), name ='index'),
url(r'^admin/', admin.site.urls),
]

这是我的第一个应用程序 urls.py:

from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static

app_name = 'home'

from . import views
urlpatterns = [

url(r'^', views.index, name ='index'),


]

这是我的第二个应用程序 urls.py:

from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static

app_name = 'about'

from . import views
urlpatterns = [

url(r'^/about/$', views.about, name ='about'),


]

最佳答案

url 模式的顺序很重要。它将返回列表顶部的任何第一个匹配模式,即使列表底部可能有更具体的模式。

还有一个模式,如:r'^' 将匹配任何内容。包括:http://127.0.0.1:8000/abc/def/ghi/ .然而,通常这样的模式会包含另一个嵌套的 urlpattern,它们通常会被某种 r'^$' 关闭,它不再匹配。

关于python - Django 不呈现第二个应用程序 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35230413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com