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-11-15SendGrid 的 Go 客户端库怎么实现同时向多个邮箱发送邮件?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么设置header 和 标签tag 呢?-icode9专业技术文章分享
- 2024-11-12Cargo deny安装指路
- 2024-11-02MongoDB项目实战:从入门到初级应用
- 2024-11-01随时随地一键转录,Google Cloud 新模型 Chirp 2 让语音识别更上一层楼
- 2024-10-25Google Cloud动手实验详解:如何在Cloud Run上开发无服务器应用
- 2024-10-24AI ?先驱齐聚 BAAI 2024,发布大规模语言、多模态、具身、生物计算以及 FlagOpen 2.0 等 AI 模型创新成果。
- 2024-10-20goland工具下,如修改一个项目的标准库SDK的版本-icode9专业技术文章分享
- 2024-10-17Go学习:初学者的简单教程
- 2024-10-17Go学习:新手入门完全指南