1.配置环境
1
2
3
4
|
安装python3 安装python3 - pip 通过pip安装django * * 如果需要使用jinja模板,需要通过pip安装django - jinja与jinja2 * * |
2. 新建django工程
1
|
django - admin startproject rcsiteproject |
其目录结构如下图所示:
3.新建app
1
2
|
python3 manage.py startapp app1 python3 manage.py startapp app2 |
4.配置app的urls
在每个app中新建urls文件
在rcsiteproject中的urls.py文件包含每个app的url。
1
2
3
4
5
|
urlpatterns = [ url(r '^admin/' , include(admin.site.urls)), url(r '^app1/' , include( 'app1.urls' )), url(r '^app2/' , include( 'app2.urls' )), ] |
5.配置setting.py
1
2
3
4
5
6
7
8
9
10
|
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
1
2
3
4
5
6
7
8
9
|
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'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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解析
1
2
3
|
installed_apps = [ 'django_jinja' ] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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' , ], }, }, ] |
上述文章有什么不之处,欢迎大家指正。
原文链接:https://blog.csdn.net/sinat_23246437/article/details/54408495