django没有在生产中加载.js静态文件 [英] django not loading .js static file in production
问题描述
我很难设置我的静态文件以供Django加载.我的UWSGI连接正常,我收到 http 200 OK
和 index.html
,但是< scipt type ="application/javascript"src ="{%static" frontend/main.js"%}"></scipt>
没有任何帮助.这很奇怪,因为 127.0.0.1:8000/static/main.js
实际上返回了 main.js
.
I have a hard time setting up my static files for Django to load.
My UWSGI connection is on point, and i receive http 200 OK
with index.html
, but
<scipt type="application/javascript" src="{% static "frontend/main.js" %}"></scipt>
gives me nothing. It'strange, because 127.0.0.1:8000/static/main.js
actually return main.js
.
我认为将此文件加载到此模板中存在问题.有什么主意吗?
I think there is a problem with loading this file into this template. Any ideas whats wrong?
(我在docker容器中运行了整个应用程序)
(I run the whole app in docker container)
要加载的 index.html
模板:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Label It</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
</head>
<body style="margin: 0">
<div id="main">
<div id="app">
</div>
</div>
<scipt type="application/javascript" src="{% static "frontend/main.js" %}"></scipt>
</body>
</html>
来自 settings.py
的一部分:
STATIC_URL = '/static/'
MEDIA_URL = '/static/media/'
STATIC_ROOT = '/vol/web/static'
MEDIA_ROOT = '/vol/web/media'
STATICFILES_DIRS = [
'/vol/web/static/frontend'
]
推荐答案
尝试更改
src ="{%static" frontend/main.js"%}"></scipt>
到
src ="{%static" main.js"%}"></scipt>
我的直觉是,如果 127.0.0.1:8000/static/main.js
有效,则必须表示'frontend/'
部分是多余的.
My hunch is if 127.0.0.1:8000/static/main.js
works it must mean the 'frontend/'
part is redundant.
这篇关于django没有在生产中加载.js静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!