简体   繁体   English

Django static 文件 (css) 不工作

[英]Django static files (css) not working

I can't seem to include my bootstrap css files for some reason.由于某种原因,我似乎无法包含我的 bootstrap css 文件。 I am quite new to Python and Django especially, so I am definitely doing something wrong.我对 Python 和 Django 特别陌生,所以我肯定做错了什么。

  • Django 1.9.2 Django 1.9.2

After reading the official Django explanation on the "Static files" management I am absolutely zero smarter:(. Here is my project folders hierarchy:在阅读了关于“静态文件”管理的官方 Django 解释后,我绝对是零聪明:(。这是我的项目文件夹层次结构:

/projectname/
    /appname/
        /static/
        |   /appname/
        |        /css/
        |        |    bootstrap.min.css
        |        |    custom.css
        |        /img/
        |        /js/
        |
        /templates/
            /includes/
                head.html
                footer.html
            index.html
            base.html

I started with the basics so I disregarded the head.html and tried with the base.html like so:我从基础开始,所以我忽略了head.html并尝试使用base.html像这样:

<title>{% block title %}{% endblock %}</title>

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

No luck.没有运气。 Here is my settings file:这是我的设置文件:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
...

STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILE_DIRS ='/users/edchigliak/documents/projects/projectname/appname/static/'

As fas as I understand, it is possible to have a "global" 'static files location' which all your projects can use, and "per app" 'static files location' which can be uses only by the app inside which base directory they reside.据我所知,可以有一个所有项目都可以使用的“全局”“静态文件位置”,以及“每个应用程序”的“静态文件位置”,它只能由它们所在的基本目录中的应用程序使用居住。

Any help appreciated!任何帮助表示赞赏!

EDIT:编辑:

This is my urls.py configuration:这是我的urls.py配置:

from django.conf.urls import url
from django.contrib import admin
from budgeteer.views import hello, hours_ahead, current_datetime

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/$', hello),
url(r'^index/$', current_datetime),
url(r'^time/plus/(\d{1,2})/$', hours_ahead),
]

I think you need to add following to your URLs:我认为您需要在您的网址中添加以下内容:

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

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

unless you work on Django server and it serves your static files.除非您在 Django 服务器上工作并且它为您的静态文件提供服务。

According you the Django docs your app structure is OK.根据您的 Django 文档,您的应用程序结构没问题。

When you will setup your prod and start serve static by Apache/Nginx/etc, than you will need to run collectstatic.当您设置 prod 并通过 Apache/Nginx/etc 启动静态服务时,您将需要运行 collectstatic。 For now it don't needed.暂时不需要。

I have similar problem (Django 1.10).我有类似的问题(Django 1.10)。 Hierarchy:等级制度:

myapp
   ...
   myapp
      ...
   blog
      migrations
      templates
      ...
   static
      blog
         style.css

So if I add <link href="{% static 'blog/css/bootstrap.min.css' %}" rel="stylesheet"> ( style.css located in dir 'blog/css') all styles won't work.因此,如果我添加<link href="{% static 'blog/css/bootstrap.min.css' %}" rel="stylesheet"> (位于目录 'blog/css' 中的style.css )所有样式都不会工作。

BUT when I delete 'css': <link href="{% static 'blog/bootstrap.min.css' %}" rel="stylesheet"> ( style.css located in dir 'blog') it's ok.但是当我删除 'css': <link href="{% static 'blog/bootstrap.min.css' %}" rel="stylesheet"> ( style.css位于目录 'blog' 中) 就可以了。

May be it help you!可能对你有帮助!

My quick guess is that you are one level up.我的快速猜测是你升级了一级。 You have your static directory nested under appname .您的static目录嵌套在appname下。 Try moving it up a level and accessing the resource directly in the browser ( http://example.com/static/appname/css/bootstrap.min.css )尝试将其向上移动一个级别并直接在浏览器中访问资源 ( http://example.com/static/appname/css/bootstrap.min.css )

I've never done app specific resources, so if that is the goal, my apologies.我从未做过特定于应用程序的资源,所以如果这是目标,我很抱歉。

what if your static link starts with just appname ?如果您的静态链接仅以appname开头怎么办?

ie, instead of即,而不是

<link href="{% static 'static/appname/css/bootstrap.min.css' %}" rel="stylesheet">

please try请尝试

<link href="{% static 'appname/css/bootstrap.min.css' %}" rel="stylesheet">

AFAIK, the string in {% static %} is the path to a static file inside the static folder. AFAIK, {% static %}的字符串是静态文件夹内静态文件的路径。

I don't have points enough to comment, so I leave my guess here.我没有足够的积分来评论,所以我在这里留下我的猜测。

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

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

You need to put this line on the outside of HTML tags.您需要将此行放在 HTML 标记的外部。

{% load static %}

I found the answer here: https://tutorial.djangogirls.org/en/css/ .我在这里找到了答案: https : //tutorial.djangogirls.org/en/css/

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

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