本篇主要讲解如何将可复用的功能作成独立的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,如需转载请自行联系原作者