如何在Django中正确地服务静态文件?论赫罗库

2024-05-29 01:43:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我的静态文件在DEBUG=True的条件下工作,但在DEBUG=False的条件下不工作

我的设置.py你知道吗

STATIC_URL = '/static/'


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

你知道吗网址.py你知道吗

from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
#....
] + static(settings.MEDIA_URL , document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

命令我试试 python管理.py收集静态 它创建了一个文件夹,但无法呈现数据

你知道吗主页.html你知道吗

<!DOCTYPE html>
{% load staticfiles %} # I try {% load static %} also
<html>
<head>
   <link rel="stylesheet" type="text/css" href="{% static 'app1css/home.css' %}">
      <title></title>
  </head>
  <body>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do 
     .</p>
     </body>
   </html>

这是我的项目结构

C:.
├───app1
│   ├───migrations
│       │   └───__pycache__
│           └───__pycache__
├───mysite
│   ├───static
│   │   └───app1css
│   └───__pycache__
├───static
│   ├───admin
│   │   ├───css
│   │   │   └───vendor
│   │   │       └───select2
│   │   ├───fonts
│   │   ├───img
│   │   │   └───gis
│   │   └───js
│   │       ├───admin
│   │       └───vendor
│   │           ├───jquery
│   │           ├───select2
│   │           │   └───i18n
│   │           └───xregexp
│   └───app1css
└───templates
    └───app

我读过许多与此相关的问题,但无法解决我的问题。我已经用这个设置(静态文件)上传了我的博客到heroku,但是它给了我SERVERERROR 500(debug=False)。我尝试将{%loadstatic%}更改为{%loadstaticfiles%},但仍然

django版本2.1.5 服务器apache python版本3.6.7


Tags: 文件djangopydebugurlsettingshtml静态

热门问题