Django加载静态文件? [英] Django loading static files?

查看:126
本文介绍了Django加载静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django没有加载我的静态文件。但是它正在加载我们在静态文件夹中的模板。另外,chrome没有看到静态文件,我甚至没有得到一个404错误,是的,他们链接在html ...但他们没有显示在网络面板中

Django is not loading my static files. However it is loading my templates which are in the static folder. Also chrome is not seeing the static files either I'm not even getting a 404 error and yes they are linked in the html...but they are not showing up in the network panel

Heres my settings.py file

Heres my settings.py file

    STATIC_ROOT = ''

    STATIC_URL = '/static/'

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

这是我的html

    <head>
        <title>MySite | Home</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" type='text/css' src='css/normalize.css'>
        <link href='http://fonts.googleapis.com/css?family=Questrial|Josefin+Sans' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" type="text/css" src='css/main.css'>
        <script src="https://maps.googleapis.com/maps/api/js"></script>
    </head>

对不起我知道这个问题已经被多次询问,我已经尝试所有这些解决方案,没有运气。我花了2天试图弄清楚这一点。

Sorry I know this question has been asked multiple times and i've tried all those solutions with no luck. I've spent 2 days trying to figure this out

推荐答案

我使用静态文件的方法基本上是在< a href =https://docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/ =nofollow> docs 。

The approach I take with static files is basically what's outlined in the docs.

在本地开发中,只要 django.contrib.staticfiles STATIC_ROOT 中指定的目录自动提供静态文件c>在您的 INSTALLED_APPS DEBUG = True

In local development, Django will serve static files automatically from the directory specified in STATIC_ROOT as long as django.contrib.staticfiles is in your INSTALLED_APPS and DEBUG = True

我的项目结构通常如下所示:

My project structure typically looks like this:

my_project/
    some_app/
    lib/
    static/  <------- STATIC_ROOT - used in production. `collectstatic` collects here
    static_assets/ <- STATICFILES_DIRS - used in local dev
        css/
        less/
        js/
        images/
    templates/  <---- TEMPLATE_DIRS
    manage.py

settings.py通常为: / p>

settings.py is typically:

INSTALLED_APPS = (
    . . .
    'django.contrib.staticfiles',
    . . .
)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static_assets'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

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

然后在模板中,您可以再次使用staticfiles应用程序的模板标签来构建stat的路径ic文件:

Then in templates, you can again use the staticfiles app's template tags to build out paths to static files:

{% load static from staticfiles %}

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

另请注意,使用< link> 标签,您需要为url而不是 src 使用 href 属性。

Also note that with <link> tags, you need to use the href property for the url instead of src.

这篇关于Django加载静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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