【发布时间】:2020-08-03 12:51:32
【问题描述】:
我的静态文件只加载在索引页面上,但是当导航到另一个页面时,例如:examle_url/page,它返回一个 404 错误,因为它是这样加载的:http://127.0.0.1:8000/categories/default/static/js/main.js,查看页面后的静态加载
settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '/static/base'),
)
urls.py "我所有的 url 都在主目录中,然后我从应用程序中导入视图"
urlpatterns = [
path('admin/', admin.site.urls),
path('profile/', profile, name="profile"),
path('', index, name="index"),
path('category/', category, name="category"),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
如果重要的话,我的加载静态文件都在 theme.html 文件中,然后在所有其他 html 文件中扩展。
任何帮助将不胜感激,在此先感谢!
【问题讨论】:
-
您应该在每个模板(或基本模板)的顶部提及
{% load static %}以加载静态文件... -
@SwetankPoddar:我不相信这是问题所在,否则这将如何首先生成 href?