【问题标题】:Who generates the default page of django welcome page?谁生成django欢迎页面的默认页面?
【发布时间】:2013-02-03 18:41:35
【问题描述】:

我刚刚设置了 django 环境,正如教程所说的那样。我输入了python manager.py runserver,它告诉我打开127.0.0.1:8000。当我打开它时,它使用正确的欢迎页面。

但这是我的问题:谁生成了这个默认的欢迎页面?因为没有views.py 并且urls.py 页面是空的。

【问题讨论】:

    标签: django python-2.7


    【解决方案1】:

    如果您的 urls.py 为空(如不包含匹配 url 的模式)并且 Django 处于调试模式(您的设置中为 DEBUG = True),则 Django 会触发您所看到的页面。

    Django 视图:

    https://github.com/django/django/blob/main/django/views/debug.py#L575-L583

    HTML 模板:

    https://github.com/django/django/blob/ca9872905559026af82000e46cde6f7dedc897b6/django/views/templates/default_urlconf.html

    【讨论】:

      【解决方案2】:

      免责声明:我使用的是 django 版本:3.2.5,在旧版本中文件内容可能会有所不同)

      第一次运行 django web 应用程序时看到的默认登录页面是“default_urlconf.html”。这是 django 添加的安全机制,而不是在没有任何默认 url/路由的情况下给出 404 错误。

      当您第一次使用 django-admin startproject <project_name> 之类的命令或使用任何 IDE 菜单控件(例如“Pycharm 专业人员”)创建 django 网站时,您会得到以下文件夹结构。此外,Debug 在您的 setting.py 文件中设置为 True (DEBUG = True)

      此外, 当路由未设置或 url.py 中的 url 映射为空时,django 框架通过其错误处理代码提供默认路由。 这就像当您尝试访问/请求 http://127.0.0.1:8000/ django 时检查该 url 是否存在(404)以及调试模式是否处于活动状态。这个请求会遍历各种 *.py 文件,如base.pyhandlers.pyexceptions.py,最后到达debug.py。所有这些文件都随虚拟环境提供或在您(在项目中安装 django)时提供。

      最终,从“exception.py”通过方法**debug.technical_404_response(request, exc)**进入“debug.py”

      def response_for_exception(request, exc):
          if isinstance(exc, Http404):
              if settings.DEBUG:
                  response = debug.technical_404_response(request, exc)
              else:
                  response = get_exception_response(request, get_resolver(get_urlconf()), 404, exc)
      

      最后感觉debug.pydef technical_404_response(request, exception) 调用def default_urlconf(request) 并最终返回默认html 页面的响应(default_urlconf.html)你在屏幕上看到的

      def default_urlconf(request):
          """Create an empty URLconf 404 error response."""
          with Path(CURRENT_DIR, 'templates', 'default_urlconf.html').open(encoding='utf-8') as fh:
              t = DEBUG_ENGINE.from_string(fh.read())
          c = Context({
              'version': get_docs_version(),
          })
      
          return HttpResponse(t.render(c), content_type='text/html')
      

      文件位置:

      • exception.py: venv/Lib/site-packages/django/core/handlers/exception.py

      • debug.py:venv/Lib/site-packages/django/views/debug.py

      • default_urlconf.html: venv/Lib/site-packages/django/views/templates/default_urlconf.html

      【讨论】:

        【解决方案3】:

        如果有人想收回(或重复使用)它,您只需将debug.default_urlconf 视图添加到您的urls

        …
        from django.views import debug
        …
        
        urlpatterns = [
            …
            path('', debug.default_urlconf),
            …
        ]
        

        【讨论】:

          【解决方案4】:

          看看django/core/handlers/base.pydjango/views/debug.py。简而言之,如果 django 得到一个 404,如果你没有设置任何路由,那么它会在 base.py 中

          if settings.DEBUG:
              from django.views import debug
              response = debug.technical_404_response(request, e)
          

          在 debug.py 中查看 technical_404_responseempty_urlconf

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-11-25
            • 2012-08-02
            • 2012-09-04
            • 2011-09-22
            • 2023-03-28
            • 2015-03-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多