您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Django静态文件无法加载

Django静态文件无法加载

您的问题是您无法在urls.py中的任何地方收听URL“ / static /”

如果您通过apache或Nginx之类的网络服务器为应用程序提供服务,那么这是正常现象,因为该网络服务器将处理静态文件本身。

为了进行开发,Django带有内置的静态服务器

到urls.py,最后添加

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

这样做是添加/ static /网址,并让您在没有网络服务器的情况下提供服务。

这相当于

url(
    regex=r'^static/(?P<path>.*)$', 
    view='django.views.static.serve', 
    kwargs={'document_root': settings.STATIC_ROOT,}
)

some people will tell you that you need to wrap the URL-rules in a “if settings.DEBUG” to use the dev-only rules, but this isnt needed at all and actually i find that to be a bad advice.

Go 2022/1/1 18:46:22 有360人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶