安装python3
安装python3-pip
通过pip安装Django
**如果需要使用Jinja模板,需要通过pip安装django-jinja与jinja2**
django-admin startproject rcsiteproject
python3 manage.py startapp app1
python3 manage.py startapp app2
在每个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')),
]
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
'app2'
)
在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',
],
},
},
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',
],
},
},
]
上述文章有什么不之处,欢迎大家指正。