如何将我自己的文件添加到Django的“静态"文件夹 [英] How to add my own files to django 'static' folder

查看:49
本文介绍了如何将我自己的文件添加到Django的“静态"文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 django静态文件文档,django的静态文件设置是这样的

I've read django static files document and made my django static files settings like this

setting.py

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'

html页面

<img src="{% static "admin/img/author.jpg" %}" alt="My image"/>

因此,如果我处理了django默认的静态文件之一,则可以正常工作.但是,如果我将自己的文件和文件夹添加到 static 文件夹中,则不会显示它.

So if I address one of django default static files, it works fine. But if I add my own file and folders to the static folder, it doesn't show it.

我尝试过

python manage.py collectstatic

但是什么都没有改变.我该如何运作?

But nothing changed. How can I make it work?

推荐答案

几件事...

STATICFILES_DIRS = (
    'path/to/files/in/development',
)

STATIC_ROOT = 'path/where/static/files/are/collected/in/production'

DEBUG = True 时,当您使用 {%static'path/to/file'%} 模板标记.

When DEBUG = True, Django will automatically serve files located in any directories within STATICFILES_DIRS when you use the {% static 'path/to/file' %} template tag.

DEBUG = False 时,Django将自动提供任何文件,并且您应从Apache,Nginx等在指定的位置提供这些文件. STATIC_ROOT .

When DEBUG = False, Django will not serve any files automatically, and you are expected to serve those files using Apache, Nginx, etc, from the location specified in STATIC_ROOT.

当您运行 $ manage.py collectstatic 时,Django将复制位于 STATICFILES_DIRS 中的所有文件,以及在第三方应用程序中名为"static"的任何目录中的文件,放在 STATIC_ROOT 指定的位置.

When you run $ manage.py collectstatic, Django will copy any and all files located in STATICFILES_DIRS and also files within any directory named 'static' in 3rd party apps, into the location specified by STATIC_ROOT.

我通常这样构造我的项目根目录:

I typically structure my project root as such:

my_project/
    /static_assets/  (specified in STATICFILES_DIRS)
        /js
        /images

    /static (specified in STATIC_ROOT)

我希望可以帮助您了解staticfiles应用程序的工作原理.

I hope that helps you understand how the staticfiles app works.

这篇关于如何将我自己的文件添加到Django的“静态"文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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