• 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,前端输入路由查看是否运行正常

     

  • 相关阅读:
    从客户端(Content="<p>666</p>")中检测到有潜在危险的 Request.Form 值。
    VS插件集
    Carmack在QUAKE3中使用的计算平方根的函数
    自动匹配HTTP请求中对应实体参数名的数据(性能不是最优)
    webapi单元测试时出现的ConfigurationManager.ConnectionStrings为空错误
    @@IDENTITY在加触发器时返回错误的ID值
    Protobuf完整实例
    Apache配置多个监听端口和不同的网站目录的简单方法[转]
    ThinkPHP 小技巧
    复选框 ajax取得后台页面
  • 原文地址:https://www.cnblogs.com/xuewei95/p/15914368.html
Copyright © 2020-2023  润新知