在Django开发中提供静态文件 [英] Serving static files in Django development

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

问题描述

我使用Django 1.4在开发过程中提供静态文件问题

I have a problem serving static files in development using Django 1.4

STATIC_ROOT = 'C:/projects/foobar/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ()

在我的模板中,我将其称为 static ,如下所示:

In my templates I refer to this as static like this:

<link rel='stylesheet' href='/static/foobar.css'>

请注意,我没有使用任何上下文处理器,因为我是硬编码 static

Notice that I'm not using any context processor as I'm hard-coding static

根据文档我看不到我在做错什么

Based on the docs I can't see what I'm doing wrong

推荐答案

确定我找到了一个解决方案:

Ok I've found a solution:

STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('C:/projects/foobar/static',)

这样我只是从模板中引用 / static / 。这是奇怪的,STATIC_ROOT需要是emtpy和STATICFILES_DIRS是什么重要的,它是反直觉的。

This way I just refer to /static/ from within the templates. It's weird that STATIC_ROOT needs to be emtpy and STATICFILES_DIRS is what counts though, it's counter-intuitive.

这不需要静态文件夹在一个应用程序内,它可以在项目的根目录中,并且不需要更改urls.py,也不需要使用collectstatic命令

This doesn't require for the static folder to be inside an app, it can just be in the project's root and it requires no change to urls.py nor the use of the collectstatic command

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

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