简体   繁体   English

Python Django静态CSS文件

[英]Python Django static css files

I know this is a stupid question, but I've read through the Django documentation a dozen times and read every related question on on here and still can't figure out what I'm doing wrong. 我知道这是一个愚蠢的问题,但是我已经阅读了Django文档十多次,并在这里阅读了每个相关的问题,但仍然无法弄清楚我在做什么错。

I'm trying to link my template CSS to a file in my static root. 我正在尝试将模板CSS链接到我的静态根目录中的文件。 Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

settings.py: settings.py:

STATIC_URL = '/static/'
STATIC_ROOT =  'C:/Users/Chris/Dropbox/MyProject/MyProject/static/styles/'

STATICFILES_DIRS = (
    "C:/Users/Chris/Dropbox/MyProject/MyProject/static/styles",
    )

in the template: 在模板中:

{% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static '/styles/style.css' %}">

urls.py urls.py

from django.conf.urls.static import static

urlpatterns = patterns('',

# my url patterns here

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

My guess is that either the static files code in urls.py is incorrect or the STATICFILES_DIRS in settings.py, but I've tried a million combinations based on other questions on stackoverflow and nothing seems to work. 我的猜测是urls.py中的静态文件代码不正确,或者settings.py中的STATICFILES_DIRS,但是我已经基于stackoverflow上的其他问题尝试了一百万种组合,但似乎没有任何效果。 I just get a 404 page not found error. 我刚收到404页面未找到错误。

Any help is appreciated. 任何帮助表示赞赏。 I've spent an embarrassing amount of time on this. 我为此花费了很多令人尴尬的时间。

You have two main problems: 您有两个主要问题:

  1. Your STATIC_ROOT directory is included in STATICFILES_DIRS . 您的STATIC_ROOT目录包含在STATICFILES_DIRS STATIC_ROOT is where the collectstatic command will copy all static files , once you are ready for deployment. 一旦准备好部署, STATIC_ROOT就是collectstatic命令将复制所有静态文件的地方 You obviously don't want this path to be any where you already have static files as all files in this path are overwritten. 您显然不希望此路径位于已经有静态文件的任何位置,因为此路径中的所有文件都会被覆盖。

  2. Your static url is including a path component already included in STATICFILES_DIRS . 您的静态网址包含STATICFILES_DIRS已包含的路径组件。 You have /MyProject/static/styles in STATICFILES_DIRS , this means that the static tag will look inside this directory for requested files. 您在STATICFILES_DIRS具有/MyProject/static/styles ,这意味着static标签将在此目录中查找请求的文件。 Now you are requesting a link to '/styles/style.css' , and so django is looking for this file: /MyProject/static/styles/styles/style.css which does not exist. 现在,您需要一个到'/styles/style.css'的链接,因此django在寻找此文件: /MyProject/static/styles/styles/style.css不存在。 To fix the problem, change the static tag to {% static 'style.css' %} . 要解决此问题,请将静态标记更改为{% static 'style.css' %}

try this in settings.py file 在settings.py文件中尝试一下

STATIC_ROOT = os.path.join(SITE_ROOT, 'templates/static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (

                         SITE_ROOT + '/templates',

)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',

)

and in urls.py file 并在urls.py文件中

urlpatterns += patterns('',
                        (r'^static/(?P<path>.*)$', 'django.views.static.serve',
                         {'document_root': os.path.join(settings.SITE_ROOT, 'templates/static')}),

                    )

hope this will help you 希望这个能对您有所帮助

Using static path maybe more intuitive. 使用静态路径可能更直观。

In settings.py declare 在settings.py中声明

STATIC_PATH = os.path.join(BASE_DIR, 'static')
# Add any additional locations of static files

STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
# os.path.join(ROOT_PATH, "public")
    STATIC_PATH,
)

Then in your template simply load the files 然后在您的模板中只需加载文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM