静态文件不再加载-Django [英] Static files doesn't load anymore - Django
问题描述
开始时,一切正常,但是现在我的静态文件无法加载.我认为这是一个非常奇怪的问题,因为在发生问题时我并没有真正接触到可能对问题产生影响的任何东西.其他一切工作正常,由于某种原因我无法获取静态文件.有时会收到200 http响应,试图像这样获取静态文件:
In the beginning everything worked perfectly fine, but now I have a problem where my static files doesn't load. I think this is a very weird problem since I didn't really touch anything that could've had an influence on the problem when it happened. Everything else is working fine, I just can't get the static files for some reason. I sometimes get a 200 http response trying to get the static files like so:
[20/Aug/2020 16:12:51] "GET /order/checkout/ HTTP/1.1" 200 2029
[20/Aug/2020 16:12:51] "GET /static/my_pizza_place/js/client.js HTTP/1.1" 200 2194
[20/Aug/2020 16:12:51] "GET /static/css/master.css HTTP/1.1" 200 80
[20/Aug/2020 16:12:51] "GET /static/css/global.css HTTP/1.1" 200 530
但是它仍然没有将样式应用于我的html代码.我通常在我的client.js文件上只收到304 http响应.我觉得目前我已经尝试了几乎所有方法,所以希望大家能帮我弄清楚问题出在哪里.
But it's still not applying the styling to my html code. I usually just get a 304 http response on my client.js file though. I feel like I have tried almost everything at this point, so I hope you guys can help me figuring out what the problem is.
- SETTINGS.PY
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
- BASE.HTML
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
...
<link rel="stylesheet" href="{% static 'css/master.css' %}">
<link rel="stylesheet" href="{% static 'css/global.css' %}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="..." crossorigin="anonymous">
<script src="{% static 'my_pizza_place/js/client.js' %}" defer></script>
</head>
<body>
<div class="container mycontent">
{% block checkout %}
{% endblock %}
</div>
</body>
</html>
- INDEX.HTML
{% extends 'base.html' %}
{% block content %}
...
{% endblock %}
- URLS.PY-项目文件夹中
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.HomePage.as_view(),name="home"),
path('customer/',include('customer.urls', namespace='customer')),
path('customer/',include('django.contrib.auth.urls')),
path('order/',include('orders.urls',namespace='order'))
]
- VIEWS.PY
class HomePage(TemplateView):
template_name = 'index.html'
-
目录结构
- 项目
- app_name
- project_name
- 静态
- css
- global.css
- master.css
- my_pizza_place
- js
- client.js
- 模板
- base.html
如果您需要更多信息,请询问.预先感谢
If you need any more information please just ask. Thanks in advance
推荐答案
您的问题很简单,您应该在每个文件中添加{%load static%}.您不必一定要在每个.html文件中链接css文件,但是在每个文件中,您都应该加载静态文件.我之前曾遇到过此问题,并且我相信这会为您解决,请确保在{%block content%}标签之前添加{%load static%}.如果您需要进一步的说明,请在下面评论.
Your problem is simply that you should add {% load static %} in each file. You don't have to necessarily link the css files in each .html file, but in each file you should load the static files. I have faced this problem before and I am sure this will solve it for you, be sure to add the {% load static %} before the {%block content %} tag. If you need further explanation, comment below.
这篇关于静态文件不再加载-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!