Home > Backend Development > Python Tutorial > Django configuration method for multiple applications to coexist

Django configuration method for multiple applications to coexist

不言
Release: 2018-06-01 14:52:03
Original
2189 people have browsed it

这篇文章主要介绍了多个应用共存的Django配置方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

1.配置环境

安装python3
安装python3-pip
通过pip安装Django
**如果需要使用Jinja模板,需要通过pip安装django-jinja与jinja2**
Copy after login

2. 新建Django工程

django-admin startproject rcsiteproject
Copy after login

其目录结构如下图所示:

3.新建app

python3 manage.py startapp app1
python3 manage.py startapp app2
Copy after login

4.配置app的urls

在每个app中新建urls文件

在rcsiteproject中的urls.py文件包含每个app的url。

urlpatterns = [
 url(r'^admin/', include(admin.site.urls)),
 url(r'^app1/', include('app1.urls')),
 url(r'^app2/', include('app2.urls')),
]
Copy after login

5.配置setting.py

INSTALLED_APPS = (
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'app1',
 'app2'
)
Copy after login

6.添加文件中共同引用的commontemplates与commonstatic文件夹

在setting中配置static及template

HERE = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join( HERE ,'media').replace('\\','/') 
MEDIA_URL = '/media/' 
STATIC_ROOT = os.path.join(HERE,'static').replace('\\','/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
 # add other path no app static 
 os.path.join(HERE,'commonstatic/').replace('\\','/'),
)
Copy after login

配置templates ‘DIRS'.

TEMPLATES = [
 {
 'BACKEND': 'django.template.backends.django.DjangoTemplates',
 'DIRS': [(os.path.join(BASE_DIR, 'commontemplates')),],
 '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',
  ],
 },
 },
Copy after login

7.配置template Jinja2解析

INSTALLED_APPS = [
 'django_jinja'
]
Copy after login

TEMPLATES = [
 {
 "BACKEND": "django_jinja.backend.Jinja2",
 'DIRS': [(os.path.join(BASE_DIR, 'commontemplates')),],
 "APP_DIRS": True,
 "OPTIONS": {
  "app_dirname": "templates",
  "match_extension": ".html",
 }
 },
 {
 'BACKEND': 'django.template.backends.django.DjangoTemplates',
 'DIRS': [(os.path.join(BASE_DIR, 'commontemplates')),],
 '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',
  ],
 },
 },
]
Copy after login

上述文章有什么不之处,欢迎大家指正。

相关推荐:

django 多数据库配置教程

从django的中间件直接返回请求的方法

The above is the detailed content of Django configuration method for multiple applications to coexist. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template