Django 1.6中的静态文件 [英] Static files in Django 1.6

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

问题描述

我可能在这里做错了,因为我仍然无法让静态文件在我的开发环境中正常运行,尽管密切关注本教程。我有一种感觉,因为它在Django 1.6中的工作方式略有不同,我只能找到以前版本的答案。

I'm probably doing multiple things wrong here because I still can't get static files to work correctly in my development environment despite closely following the tutorial. I have a feeling it's because it works slightly differently in Django 1.6, and I can only find answers for previous versions.

这是我的目录结构:

mysite
├───app1
├───mysite
│   └───templates
├───resources
├───static
│   ├───css
│   ├───fonts
│   └───js
└───app2

我已安装的应用程序,以证明我有静态文件:

My installed apps, to prove I have staticfiles on:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app1',
    'app2',
)

我的模板和静态文件设置:

My template and static file settings:

 # Templates
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, "mysite/templates"),
)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

我甚至在我的 urls.py 中提出建议:

I even did this in my urls.py as suggested:

urlpatterns = patterns('',
    ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

最后,我的请求:

{% load staticfiles %}
<link href="{% static "css/core.css" %}" rel="stylesheet">

如果我直接导​​航到 http://127.0.0.1/static/ css / core.css ,我得到'css\core.css'找不到

If I navigate directly to http://127.0.0.1/static/css/core.css, I get 'css\core.css' could not be found

请告诉我我做错了什么= [

Please tell me what I did wrong =[

推荐答案

由于静态目录不活的应用程序( app1 app2 在您的情况下),django找不到静态目录。所以使用你当前的结构,你需要将静态目录添加到 STATICFILES_DIRS

Since the static directory does not "live" in one of the apps (app1, app2 in your case), django can't find the static directory. So with your current structure you need to add the static directory to the STATICFILES_DIRS.

从文档中: p>

From the documentation:


您的项目可能还会具有与特定应用程序无关的静态资源,除了在应用程序中使用静态/目录您可以在设置文件中定义目录列表(STATICFILES_DIRS),Django还将查找静态文件。

"Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files."

这个:

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

请参阅:
https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATICFILES_DIRS

希望这有帮助。

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

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