{"id":1121,"date":"2024-12-22T19:21:49","date_gmt":"2024-12-22T11:21:49","guid":{"rendered":"http:\/\/www.chan.ink\/?p=1121"},"modified":"2024-12-22T19:21:51","modified_gmt":"2024-12-22T11:21:51","slug":"%e3%80%90django%e6%ba%90%e7%a0%81%e3%80%91include%e7%9a%84%e6%9c%ac%e8%b4%a8","status":"publish","type":"post","link":"http:\/\/www.chan.ink\/index.php\/2024\/12\/22\/%e3%80%90django%e6%ba%90%e7%a0%81%e3%80%91include%e7%9a%84%e6%9c%ac%e8%b4%a8\/","title":{"rendered":"\u3010Django\u6e90\u7801\u3011include\u7684\u672c\u8d28"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>def include(arg, namespace=None):\n    app_name = None\n    if isinstance(arg, tuple):\n        # Callable returning a namespace hint.\n\u6ce8\u91ca # Callable returning a namespace hint \u7684\u610f\u601d\u662f\uff1a\u4e0b\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u201c\u53ef\u8c03\u7528\u5bf9\u8c61\u201d\uff08\u6bd4\u5982\u51fd\u6570\u3001lambda \u8868\u8fbe\u5f0f\u6216\u53ef\u8c03\u7528\u7684\u7c7b\uff09\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u4f1a\u8fd4\u56de\u4e00\u4e2a\u201c\u547d\u540d\u7a7a\u95f4\u63d0\u793a\u201d\u3002\n        try:\n            urlconf_module, app_name = arg\n        except ValueError:\n            if namespace:\n                raise ImproperlyConfigured(\n                    'Cannot override the namespace for a dynamic module that '\n                    'provides a namespace.'\n                )\n            raise ImproperlyConfigured(\n                'Passing a %d-tuple to include() is not supported. Pass a '\n                '2-tuple containing the list of patterns and app_name, and '\n                'provide the namespace argument to include() instead.' % len(arg)\n            )\n    else:\n        # No namespace hint - use manually provided namespace.\n        urlconf_module = arg\n\n    if isinstance(urlconf_module, str):\n        urlconf_module = import_module(urlconf_module)\n    patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)\n    app_name = getattr(urlconf_module, 'app_name', app_name)\n    if namespace and not app_name:\n        raise ImproperlyConfigured(\n            'Specifying a namespace in include() without providing an app_name '\n            'is not supported. Set the app_name attribute in the included '\n            'module, or pass a 2-tuple containing the list of patterns and '\n            'app_name instead.',\n        )\n    namespace = namespace or app_name\n    # Make sure the patterns can be iterated through (without this, some\n    # testcases will break).\n    if isinstance(patterns, (list, tuple)):\n        for url_pattern in patterns:\n            pattern = getattr(url_pattern, 'pattern', None)\n            if isinstance(pattern, LocalePrefixPattern):\n                raise ImproperlyConfigured(\n                    'Using i18n_patterns in an included URLconf is not allowed.'\n                )\n    return (urlconf_module, app_name, namespace)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe urlconf_module\u662f\u201dapps.api.urls\u201c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u5047\u8bbeapps.api.urls\u4e2d\u6709app_name\u548curl_parttens\u4e24\u4e2a\u503c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">urlconf_module = import_module(urlconf_module) #\u76f8\u5f53\u4e8eimport apps.api import urls,\u800curlconf_module\u5373\u662furls<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e5f\u5c31\u53ef\u4ee5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">urlconf_module.app_name \u7b49\u540c\u4e8egetattr(urlconf_module,&#8217;app_name&#8217;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">urlconf_module.url_parttens\u7b49\u540c\u4e8egetattr(urlconf_module,&#8217;url_parttens&#8217;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5047\u8bbe urlconf_module\u662f\u201dapps.api.urls\u201c \u518d\u5047\u8bbeapps.api.urls\u4e2d\u6709app [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1121","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/1121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/comments?post=1121"}],"version-history":[{"count":1,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"predecessor-version":[{"id":1122,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/1121\/revisions\/1122"}],"wp:attachment":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}