stark元件前戲之django路由分發的本質

2022-09-22 03:36:12 字數 3295 閱讀 9218

之前的路由分發書寫:

")檢視include原始碼

def include(arg, namespace=none):

ifisinstance(arg, tuple):

#callable returning a namespace hint.

try:

except

valueerror:

ifnamespace:

raise

improperlyconfigured(

'cannot override the namespace for a dynamic module that ''

provides a namespace.')

raise

improperlyconfigured(

'passing a %d-tuple to include() is not supported. pass a ''

''provide the namespace argument to include() instead.

' %len(arg)

)else

:

#no namespace hint - use manually provided namespace.

urlconf_module =arg

ifisinstance(urlconf_module, str):

urlconf_module =import_module(urlconf_module)

patterns = getattr(urlconf_module, '

urlpatterns

', urlconf_module)

'

if namespace and

not

raise

improperlyconfigured(

''''

'module, or pass a 2-tuple containing the list of patterns and ''',)

namespace = namespace or

#make sure the patterns can be iterated through (without this, some

#testcases will break).

ifisinstance(patterns, (list, tuple)):

for url_pattern in

patterns:

pattern = getattr(url_pattern, '

pattern

', none)

ifisinstance(pattern, localeprefixpattern):

raise

improperlyconfigured(

'using i18n_patterns in an included urlconf is not allowed.')

return

#

路由分發第二種寫法

from django.urls import

re_path

urls

urlpatterns =[

path(

'admin/

', admin.site.urls),

re_path(r

'^web/

']

在路由分發include中打上斷點,以debug模式啟動django,檢視其呼叫棧

結論:

#

如果第乙個引數有urls.urlpatterns屬性,那麼子路由就從該屬性中獲取

#如果第乙個引數無urls.urlpatterns屬性,那麼子路由就是第乙個引數

路由第三種寫法djangoproject/urls.py

from django.urls import

re_path

views

urlpatterns =[

re_path(r

'^web/

', ([

re_path(r

'^index/$

', views.index),

re_path(r

'^home/$

', views.home),

], none, none))

#]

正常啟動django,前端輸入路由檢視是否執行正常

CRM專案之stark元件 2

那麼從今天開始呢,我們就要開始設計屬於我們自己的admin元件,起個名字就叫stark吧 當然你願意叫什麼都可以 仿照admin元件實現流程,stark元件要實現四件事情 在settings配置檔案中,確保 django.contrib.admin django.contrib.auth djang...

crm專案 stark元件分析

stark元件 這個stark元件是非常神奇的 1,獨立的乙個元件 2,沒有model 3,沒有views stark元件 stark元件站點類 這個是乙個重點類,應該是研究這個元件的起點 做了幾件事 1,模仿admin,利用了單例模式,2,模仿admin,可以對每乙個表進行註冊 這一步引數就是模型...

day26 stark元件開發之關鍵搜尋功能

思路 服務v1.py模組匯入 from django.db.models import q新增函式篩選功能 search list def get search list self return self.search list changelist view函式新增篩選功能邏輯 stark元件中各...