Django 找不到静态文件.需要第二双眼睛,我快疯了 [英] Django cannot find static files. Need a second pair of eyes, I'm going crazy

查看:23
本文介绍了Django 找不到静态文件.需要第二双眼睛,我快疯了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 不会提供我的静态文件.这是返回的错误:

Django will not serve my static files. Here's the error returned:

[13/Jun/2014 06:12:09] "GET /refund/ HTTP/1.1" 200 2927
[13/Jun/2014 06:12:09] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 1667
[13/Jun/2014 06:12:09] "GET /static/css/cover.css HTTP/1.1" 404 1643
[13/Jun/2014 06:12:09] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 1661
[13/Jun/2014 06:12:09] "GET /static/assets/js/docs.min.js HTTP/1.1" 404 1667  
[13/Jun/2014 06:12:09] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 1661
[13/Jun/2014 06:12:09] "GET /static/assets/js/docs.min.js HTTP/1.1" 404 1667

这是我用于引导程序的 base.html 的相关部分:

Here's the relevant part of my base.html for bootstrap:

!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../assets/ico/favicon.ico">

<title>Cover Template for Bootstrap</title>

<!-- Bootstrap core CSS -->
{
<link href="{% static "css/bootstrap.min.css"%}" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="{% static "css/cover.css" %}" rel="stylesheet">

来自 settings.py 的相关代码(注意:模板工作正常.)

Relevant code from settings.py (Note: The templates worked just fine.)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

STATIC_URL = '/static/'

STATIC_ROOT = ''

这是我的 django 项目的布局方式:

And here's how my django project is laid out:

  • 管理员
  • db.sqlite3
  • 项目名称
  • manage.py
  • 应用名称
  • 静态
    • css
      • ...
      • ...
      • base.html

      任何帮助都将不胜感激.我不知道我可能做错了什么,我已经看了一百万次代码,很明显这一定是我对 Django 如何提供静态文件的理解有差距;但是我之前已经这样做了,没有任何问题.

      Any help at all would be greatly appreciated. I don't know what I could possibly have done wrong, I've looked at the code a million times and clearly it must be a gap in my understanding of how Django serves static files; however I have done this previously with no issues.

      推荐答案

      执行以下操作:

      1. 如果您处于调试状态,请在 settings.py 中设置 STATICFILES_DIRS = ("path/to/static") 变量.然后它应该在调试模式下工作.

      如果您希望它也能在部署模式下工作,请在 settings.py 中设置 STATIC_ROOT = ("path/to/static_root") 变量.然后,执行python manage.py collectstatic,它应该也能工作.

      If you want it to also work in deploy mode, set STATIC_ROOT = ("path/to/static_root") variable in the settings.py. Then, execute python manage.py collectstatic and it should also work.

      现在,为了更好地理解 django 如何管理静态文件:

      And now, for a better understanding how django manages static files:

      • Django 有一些预定义的位置来查找和收集静态文件,您可以使用 settings.py 中的 STATICFILES_FINDERS 指定在哪里可以找到它们.默认情况下,它会在您的应用内部查找静态文件夹.您可以通过设置 STATICFILES_DIRS 变量(路径列表或元组)告诉 Django 在其他部分查找静态文件.

      • Django has some predefined places to look for static files and collect them, you specify where to find them with the STATICFILES_FINDERS in your settings.py. By default, it looks for static folder inside your apps. You can tell Django to look for static files in other parts by setting the STATICFILES_DIRS variable (list or tuple of paths).

      在 DEBUG 模式下,静态文件是从此路径(而不是从您收集文件的 static_root 中)选取的.

      In DEBUG mode, staticfiles are picked from this paths (not from the static_root where you collect your files).

      当您执行 python manage.py collectstatic 时,Django 会遍历所有可以找到静态文件的目录,并将它们放在您的静态根目录中.在部署模式下运行时,将从该目录提供静态文件.

      When you execute python manage.py collectstatic, Django goes through all directories where static files can be found, and places them in your static root. When you run in deploy mode, static files are served from this directory.

      PS:我通常做的是创建一个名为 common 的应用程序,并在其中创建一个静态目录,以便为我的项目(以及模板)放置所有常见的 css、js.这样,我就不必指定 STATICFILES_DIRS 变量.

      PS: What I normally do is to create an app called common and create a static dir inside to place all common css, js for my project (and also for templates). This way, I don't have to specify the STATICFILES_DIRS variable.

      希望现在清楚了=)

      这篇关于Django 找不到静态文件.需要第二双眼睛,我快疯了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆