我添加了第二个应用程序:
python manage.py startapp about
在我的项目中。但是现在它正在渲染我的第一个应用程序视图,无论我放置什么URL。
实施例。 http://127.0.0.1:8000/aldsjfal/asdfadsfa/adsfasdf/adfadsf/
这是我的settings.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'),
]
答案 0 :(得分:1)
网址模式的顺序很重要。它将返回列表顶部的任何第一个匹配模式,即使列表底部可能有更具体的模式。
也是一种模式:r’^‘
将匹配任何内容。包括:http://127.0.0.1:8000/abc/def/ghi/。但是,通常这种模式将包含另一个嵌套的urlpattern,它们通常会被某种r'^$'
关闭,而这些只与test()
匹配。
答案 1 :(得分:0)
您需要在INSTALLED_APPS下的settings.py文件中添加“about”应用。像这样:
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',
'about',
]
答案 2 :(得分:0)
转到url.py
并配置 网址,如:
urlpatterns = patterns(‘’,
url(r’^$’, HomePageView.as_view(), name=‘home’),
url(r’^about/‘, include(‘blog.urls’)),
...
)
转到settings.py
并添加更多最终模块:
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',
'new_module',
]