diango视图url管理
(1)Including other URLconfs
比如一个website项目urls.py, include了其他的urls:
1
2
3
4
5
6
7
8
|
from django.conf.urls import include, url
urlpatterns = [
# ... snip ...
url(r '^community/' , include( 'django_website.aggregator.urls' )),
url(r '^contact/' , include( 'django_website.contact.urls' )),
# ... snip ...
] |
注意include之前的正则表达式没有终止符 $ 而是 /
当调用clude时,把url前边正则表达式匹配到的chop off(砍掉),剩下的string传递到include的urls中进行进一步操作。
另一个例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
from django.conf.urls import include, url
from apps.main import views as main_views
from credit import views as credit_views
extra_patterns = [
url(r '^reports/$' , credit_views.report),
url(r '^reports/(?P<id>[0-9]+)/$' , credit_views.report),
url(r '^charge/$' , credit_views.charge),
] urlpatterns = [
url(r '^$' , main_views.homepage),
url(r '^help/' , include( 'apps.help.urls' )),
url(r '^credit/' , include(extra_patterns)),
] |
该例中 url: credit/reports/ 对应的方法是credit_views.report
(1)Including other URLconfs
比如一个website项目urls.py, include了其他的urls:
1
2
3
4
5
6
7
8
|
from django.conf.urls import include, url
urlpatterns = [
# ... snip ...
url(r '^community/' , include( 'django_website.aggregator.urls' )),
url(r '^contact/' , include( 'django_website.contact.urls' )),
# ... snip ...
] |
注意include之前的正则表达式没有终止符 $ 而是 /
当调用clude时,把url前边正则表达式匹配到的chop off(砍掉),剩下的string传递到include的urls中进行进一步操作。
另一个例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
from django.conf.urls import include, url
from apps.main import views as main_views
from credit import views as credit_views
extra_patterns = [
url(r '^reports/$' , credit_views.report),
url(r '^reports/(?P<id>[0-9]+)/$' , credit_views.report),
url(r '^charge/$' , credit_views.charge),
] urlpatterns = [
url(r '^$' , main_views.homepage),
url(r '^help/' , include( 'apps.help.urls' )),
url(r '^credit/' , include(extra_patterns)),
] |
该例中 url: credit/reports/ 对应的方法是credit_views.report