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

Django configuration method for multiple applications to coexist

不言
不言Original
2018-06-01 14:52:032095browse

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

1.配置环境

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

2. 新建Django工程

django-admin startproject rcsiteproject

其目录结构如下图所示:

3.新建app

python3 manage.py startapp app1
python3 manage.py startapp app2

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')),
]

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'
)

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('\\','/'),
)

配置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',
  ],
 },
 },

7.配置template Jinja2解析

INSTALLED_APPS = [
 'django_jinja'
]

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',
  ],
 },
 },
]

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

相关推荐:

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!

Statement:
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