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

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

问题描述

Django将不会提供静态文件。这是返回的错误

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

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

 !DOCTYPE html> 
< html lang =en>
{%load static%}
< head>
< meta charset =utf-8>
< meta http-equiv =X-UA兼容content =IE = edge>
< meta name =viewportcontent =width = device-width,initial-scale = 1>
< meta name =descriptioncontent =>
< meta name =authorcontent =>
< link rel =快捷图标href =../../ assets / ico / favicon.ico>

< title> Bootstrap的封面模板< / title>

<! - Bootstrap核心CSS - >
{
< link href ={%staticcss / bootstrap.min.css%} =stylesheet>

<! - 此模板的自定义样式 - >
< link href ={%staticcss / cover.css%} =stylesheet>

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

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


STATIC_URL ='/ static /'

STATIC_ROOT =''

这是我的django项目如何布局:




  • admin

  • db.sqlite3

  • 项目名称

  • manage.py

  • 应用名称

  • static

    • css

      • ...


    • dist

      • ...



  • 模板

    • base.html




任何帮助都将不胜感激。我不知道我可能做错了什么,我已经看了一百万次的代码,显然这对我对Django如何提供静态文件的了解是一个差距。不过我以前做过这件事没有问题。

解决方案

执行以下操作:


  1. 如果您在DEBUG中,请在settings.py中设置STATICFILES_DIRS =(path / to / static)变量。它应该在调试模式下工作


  2. 如果您希望它在部署模式下工作,请将STATIC_ROOT =( path.py / to / static_root)变量。然后,执行 python manage.py collectstatic ,它也应该可以工作。



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




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


  • 在DEBUG模式下,选择静态文件从这个路径(不是从你收集文件的static_root)。


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




PS:我通常会做的是创建一个名为common的应用程序,并在其中创建一个静态目录,以将所有常见的css,js作为我的项目(也适用于模板)。这样,我不必指定STATICFILES_DIRS变量。



希望很清楚现在=)。


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

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">

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

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

STATIC_URL = '/static/'

STATIC_ROOT = ''

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

  • admin
  • db.sqlite3
  • project name
  • manage.py
  • app name
  • static
    • css
      • ...
    • dist
      • ...
  • template
    • base.html

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.

解决方案

do the following:

  1. If you are in DEBUG, set STATICFILES_DIRS = ("path/to/static") variable in your settings.py. It should then work only in DEBUG mode.

  2. 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.

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

  • 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).

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

  • 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: 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.

Hope it is clear now =).

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

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