Django使用心得(三)

简介:

本篇主要讲解如何将可复用的功能作成独立的App,并从主Project中分离出来,便于以后加入到任意Project中。

下面以一个简单的例子来说明如何物理上分离各个可复用的App

  • 建立1个主Project和3个子App
  • 简单的实现3个子App
  • 关联主工程和3个子App

1. 建立1个主Project和3个子App

首先建立一个django project,名为siteWithApps。建立方法参见Django使用心得(一)
然后建立3个子工程:(3个子工程没有实际的功能,只是模拟如何在django中分离App)
subApp1: 模拟权限认证功能,认证成功后返回siteWithApps
subApp2: 无实际功能,可返回siteWithApps
subApp3: 无实际功能,可返回siteWithApps

2. 简单的实现3个子App

3个子App的建立也非常简单,只需建立一个文件夹,然后在文件夹中新建3个空文件,文件名分别为__init__.py,urls.py,views.py,本次不涉及数据库,所以没有建立models.py。
注意,这里为了从物理上也分离这3个App,将这3个App放在与siteWithApps不同文件夹中了。整个工程的文件夹结构请参照本文最后的部分。

3个子App分别独立实现,与主Project没有任何联系,方便移植到其他Project中复用。
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# subApp1:
 
# file : views.py
 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from  django.shortcuts import  render_to_response
 
 
def  Login( request ):
 
     name =  request.POST.get( 'user_name' , '' )
     pwd =  request.POST.get( 'user_pass' , '' )
     result =  False
     if  name = =  'admin'  and  pwd = =  '123' :
         result =  True
 
     return  render_to_response( 'subApp1/check_login.html' ,
             { 'user_name'  : name,
               'user_pass' : pwd,
               'result'  : result
             })
 
 
# file : urls.py
 
from  django.conf.urls.defaults import  *
 
urlpatterns =  patterns('',
     (r '^$' , 'subApp1.views.Login'  ),
 
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# subApp2:
 
# file: views.py
 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from  django.shortcuts import  render_to_response
 
def  App( request ):
     return  render_to_response( 'subApp2/subApp2.html'  )
 
 
# file: urls.py
 
from  django.conf.urls.defaults import  *
 
urlpatterns =  patterns('',
     (r '^$' , 'subApp2.views.App'  ),
 
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# subApp3:
 
# file: views.py
 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from  django.shortcuts import  render_to_response
 
def  App( request ):
     return  render_to_response( 'subApp3/subApp3.html'  )
 
 
# file: urls.py
 
from  django.conf.urls.defaults import  *
 
urlpatterns =  patterns('',
     (r '^$' , 'subApp3.views.App'  ),
 
)

3. 关联主工程和3个子App

通过主Project的urls.py,与3个子App关联起来。耦合性非常低,可以方便的追加新的App或删除已有的App。
主Project的urls.py如下:

1
2
3
4
5
6
7
8
9
10
from  django.conf.urls.defaults import  *
 
urlpatterns =  patterns('',
     (r '^siteWithApps/$' , 'siteWithApps.views.Init'  ),
     (r '^siteWithApps/apps/$' , 'siteWithApps.views.Contents'  ),
 
     (r '^siteWithApps/logincheck/$' , include( 'subApp1.urls' ) ),
     (r '^siteWithApps/app2/$' , include( 'subApp2.urls' ) ),
     (r '^siteWithApps/app3/$' , include( 'subApp3.urls' ) ),
)

需要注意的一点是,为了能识别位于不同文件夹下的子App,需要在主Project的settings.py开始部分加入下面一段。

1
2
3
4
5
6
7
8
# add for add isolate app
import  sys
 
def  AddToPythonPath():
     sys.path.append(r "D:/vim/python/django/django-apps/" )
 
AddToPythonPath()
# end for add

这样urls.py中的include就不会报错了。

主Project和子App的template如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// file : siteWithApps->base.html
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
     <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
         <title>{% block title %}{% endblock %}</title>
     </head>
     <body>
         {% if result %}
         <a href="/siteWithApps/apps/"><h2>Go to Avilable APPS</h2></a>
         {% else %}
         <form action="/siteWithApps/logincheck/" method="post">
             <label for="user_name">用户名:</label>
             <input type="text" id="user_name" name="user_name" />
             <label for="user_pass">密码  :</label>
             <input type="text" id="user_pass" name="user_pass" />
             <br />
 
             <input type="submit" value="登录" />
         </form
         {% endif %}
         {% block login_result %}{% endblock %}
 
     </body>
</html>
 
// file : siteWithApps->apps.html
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
     <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
         <title>{% block title %}{% endblock %}</title>
     </head>
     <body>
     <h2>Go to Avilable APPS</h2>
         <ul>
             <li>
                 <a href="/siteWithApps/app2/">subApp2</a>
             </li>
             <li><a href="/siteWithApps/app3/">subApp3</a></li>
         </ul>
         {% block subApp %}{% endblock %}
     </body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// file : subApp1->check_login.html
 
{% extends "siteWithApps/base.html"  %}
 
{% block title %}LoginCheck{% endblock %}
 
{% block login_result %}
<h2>输入的用户名和密码分别为:{{ user_name }} 和 {{ user_pass }}</h2>
<label for="login_result">登录结果:</label>
{% if result %}
<h1>登录成功</h1>
{% else %}
<h1>登录失败</h1>
{% endif %}
{% endblock %}
1
2
3
4
5
6
7
8
// file : subApp2->subApp2.html
 
{% extends "siteWithApps/apps.html"  %}
{% block title %}subApp 2{% endblock %}
 
{% block subApp %}
<h1>This sub App 2</h1>
{% endblock %}
1
2
3
4
5
6
7
8
// file : subApp3->subApp3
 
{% extends "siteWithApps/apps.html"  %}
{% block title %}subApp 3{% endblock %}
 
{% block subApp %}
<h1>This sub App 3</h1>
{% endblock %}

整个工程的目录如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
D:\Vim\python\django
     |-django-apps
     |     |
     |     |-subApp1
     |     |     |-urls.py
     |     |     |-views.py
     |     |     `-__init__.py
     |     |
     |     |-subApp2
     |     |     |-urls.py
     |     |     |-views.py
     |     |     `-__init__.py
     |     |
     |     `-subApp3
     |           |-urls.py
     |           |-views.py
     |           `-__init__.py
     |
     |-django-templates
     |     |-siteWithApps
     |     |     |-apps.html
     |     |     `-base.html
     |     |
     |     |-subApp1
     |     |     `-check_login.html
     |     |
     |     |-subApp2
     |     |     `-subApp2.html
     |     |
     |     `-subApp3
     |           `-subApp3.html
     |
     `-siteWithApps
           |-manage.py
           |-settings.py
           |-urls.py
           |-views.py
           `-__init__.py


本文转自wang_yb博客园博客,原文链接:http://www.cnblogs.com/wang_yb/archive/2011/04/30/2033373.html,如需转载请自行联系原作者
目录
打赏
0
0
0
0
51
分享
相关文章
【优秀python web毕设案例】基于协同过滤算法的酒店推荐系统,django框架+bootstrap前端+echarts可视化,有后台有爬虫
本文介绍了一个基于Django框架、协同过滤算法、ECharts数据可视化以及Bootstrap前端技术的酒店推荐系统,该系统通过用户行为分析和推荐算法优化,提供个性化的酒店推荐和直观的数据展示,以提升用户体验。
269 1
【优秀python web毕设案例】基于协同过滤算法的酒店推荐系统,django框架+bootstrap前端+echarts可视化,有后台有爬虫
基于爬虫和机器学习的招聘数据分析与可视化系统,python django框架,前端bootstrap,机器学习有八种带有可视化大屏和后台
本文介绍了一个基于Python Django框架和Bootstrap前端技术,集成了机器学习算法和数据可视化的招聘数据分析与可视化系统,该系统通过爬虫技术获取职位信息,并使用多种机器学习模型进行薪资预测、职位匹配和趋势分析,提供了一个直观的可视化大屏和后台管理系统,以优化招聘策略并提升决策质量。
366 4
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第27天】本文介绍了Django框架在Python Web开发中的应用,涵盖了Django与Flask等框架的比较、项目结构、模型、视图、模板和URL配置等内容,并展示了实际代码示例,帮助读者快速掌握Django全栈开发的核心技术。
342 45
植物病害识别系统Python+卷积神经网络算法+图像识别+人工智能项目+深度学习项目+计算机课设项目+Django网页界面
植物病害识别系统。本系统使用Python作为主要编程语言,通过收集水稻常见的四种叶片病害图片('细菌性叶枯病', '稻瘟病', '褐斑病', '稻瘟条纹病毒病')作为后面模型训练用到的数据集。然后使用TensorFlow搭建卷积神经网络算法模型,并进行多轮迭代训练,最后得到一个识别精度较高的算法模型,然后将其保存为h5格式的本地模型文件。再使用Django搭建Web网页平台操作界面,实现用户上传一张测试图片识别其名称。
212 22
植物病害识别系统Python+卷积神经网络算法+图像识别+人工智能项目+深度学习项目+计算机课设项目+Django网页界面
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第26天】本文详细介绍了如何在Django框架下进行全栈开发,包括环境安装与配置、创建项目和应用、定义模型类、运行数据库迁移、创建视图和URL映射、编写模板以及启动开发服务器等步骤,并通过示例代码展示了具体实现过程。
146 2
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
85 1
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
91 4
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等