stark组件前戏之django路由分发的本质
2022/2/20 6:29:39
本文主要是介绍stark组件前戏之django路由分发的本质,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
之前的路由分发书写:
djangoProject/urls.py
from django.urls import include, re_path urlpatterns = [ re_path(r'^web/', include(("app01.urls", "app01"))), ]
app01/urls.py
from django.urls import re_path from app01 import views urlpatterns = [ re_path(r'^index/$', views.index), re_path(r'^home/$', views.home), ]
app01/views.py
from django.shortcuts import render, HttpResponse # Create your views here. def index(request): return HttpResponse("index") def home(request): return HttpResponse("home")
查看include源码
def include(arg, namespace=None): app_name = None if isinstance(arg, tuple): # Callable returning a namespace hint. try: urlconf_module, app_name = arg except ValueError: if namespace: 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 ' '2-tuple containing the list of patterns and app_name, and ' 'provide the namespace argument to include() instead.' % len(arg) ) else: # No namespace hint - use manually provided namespace. urlconf_module = arg if isinstance(urlconf_module, str): urlconf_module = import_module(urlconf_module) patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module) app_name = getattr(urlconf_module, 'app_name', app_name) if namespace and not app_name: raise ImproperlyConfigured( 'Specifying a namespace in include() without providing an app_name ' 'is not supported. Set the app_name attribute in the included ' 'module, or pass a 2-tuple containing the list of patterns and ' 'app_name instead.', ) namespace = namespace or app_name # Make sure the patterns can be iterated through (without this, some # testcases will break). if isinstance(patterns, (list, tuple)): for url_pattern in patterns: pattern = getattr(url_pattern, 'pattern', None) if isinstance(pattern, LocalePrefixPattern): raise ImproperlyConfigured( 'Using i18n_patterns in an included URLconf is not allowed.' ) return (urlconf_module, app_name, namespace) # 返回值是一个有三个元素的元组,urlconf_module会等于'app01.urls',urlconf_module = import_module(urlconf_module)相当于urlconf_module会等于from app01 import urls,也就是说urlconf_module会等于一个文件对象,即urls
路由分发第二种写法
from django.urls import re_path from app01 import urls urlpatterns = [ path('admin/', admin.site.urls), re_path(r'^web/', (urls, app_name, namespace)), ]
在路由分发include中打上断点,以debug模式启动Django,查看其调用栈
结论:
# 如果第一个参数有urls.urlpatterns属性,那么子路由就从该属性中获取 # 如果第一个参数无urls.urlpatterns属性,那么子路由就是第一个参数
路由第三种写法
djangoProject/urls.py
from django.urls import re_path from app01 import views urlpatterns = [ re_path(r'^web/', ([ re_path(r'^index/$', views.index), re_path(r'^home/$', views.home), ], None, None)) # 可删除app01下的urls.py文件 ]
正常启动Django,前端输入路由查看是否运行正常
这篇关于stark组件前戏之django路由分发的本质的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24MongoDB资料:新手入门完全指南
- 2024-12-20go-zero 框架的 RPC 服务 启动start和停止 底层是怎么实现的?-icode9专业技术文章分享
- 2024-12-19Go-Zero 框架的 RPC 服务启动和停止的基本机制和过程是怎么实现的?-icode9专业技术文章分享
- 2024-12-18怎么在golang中使用gRPC测试mock数据?-icode9专业技术文章分享
- 2024-12-15掌握PageRank算法核心!你离Google优化高手只差一步!
- 2024-12-15GORM 中的标签 gorm:"index"是什么?-icode9专业技术文章分享
- 2024-12-11怎么在 Go 语言中获取 Open vSwitch (OVS) 的桥接信息(Bridge)?-icode9专业技术文章分享
- 2024-12-11怎么用Go 语言的库来与 Open vSwitch 进行交互?-icode9专业技术文章分享
- 2024-12-11怎么在 go-zero 项目中发送阿里云短信?-icode9专业技术文章分享
- 2024-12-11怎么使用阿里云 Go SDK (alibaba-cloud-sdk-go) 发送短信?-icode9专业技术文章分享