【问题标题】:List of correct urls not displayed in browser when in django debug mode在 django 调试模式下,浏览器中未显示的正确 url 列表
【发布时间】:2020-10-12 16:51:17
【问题描述】:

通常在使用 django 的调试模式下,如果您输入错误的 url,浏览器会显示一条错误消息,其中包含可用 url 列表。 喜欢它:

Request Method: GET
Request URL:    http://127.0.0.1:8000/blorp
Using the URLconf defined in bricole.urls, Django tried these URL patterns, in this order:
 
admin/
accounts/
[name='home']
accounts/
simple/
(etc...)

但在我的一个项目中,通过输入错误的网址,我得到以下结果:

Request Method: GET
Request URL:    <a href="http://127.0.0.1:8000/phot" target="_blank">http://127.0.0.1:8000/phot</a>
Raised by:  django.views.static.serve
“C:\Users\Lou\PycharmProjects\photoglide\phot” does not exist

并且它不显示可用 url 的列表。

在调试中,浏览器中不显示url列表

我使用 venv(django 3.1.2 和 python 3.8 和 pycharm)

我将 .py 设置与一个项目的设置进行了比较,该项目的可用 url 列表在出现 url 错误时正确显示,唯一的区别似乎是:

url调试工作的项目:

STATIC_URL = '/static/'
STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))]
 
STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles'))
STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

...以及显示url列表的位置(url调试正常的项目):

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

似乎有些东西是静态的。当它不起作用时,它会告诉: 提出者:django.views.static.serve

但即使将项目的静态部分替换为它不工作的项目的静态部分,它也不会改变任何东西。

有没有人有线索并好心向我指出?

谢谢你,晚上好。

【问题讨论】:

    标签: python django url pycharm


    【解决方案1】:

    最后通过替换它在 init.py 中修复:

    BASE_DIR = Path(__file__).resolve().parent.parent
    

    这样:

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    

    也可以通过替换:

    STATIC_URL = '/static/'
    STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))]
    STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles'))
    STATICFILES_FINDERS = [
        "django.contrib.staticfiles.finders.FileSystemFinder",
        "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    ]
    

    这样:

    STATIC_URL = '/static/'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 2012-08-16
      相关资源
      最近更新 更多