如何正确指向Django中的静态图像 [英] how to point correctly to static image in django

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

问题描述

我有一个渲染图像的模板:

I have a template that renders an image:

{% load staticfiles %}

<img src="{% static "img/logo.png" %}" alt="My image"/>

图片链接已损坏,但指向:

The image link is broken, but it points to:

localhost/static/img/logo.png

为使此图像正确显示,我需要为static_root,static_url和STATICFILES_DIRS设置什么值?

What are values I need to set for static_root, static_url, and STATICFILES_DIRS to get this image to show up correctly?

这是我的目录结构:

myprojectname(顶层)

myprojectname (top level)

--- myprojectname

--- myprojectname

--- --- myproectname

--- --- myproectname

-------设置

- --- --- --- base.py(setting.py)

--- --- --- --- base.py (setting.py)

-----静态

------ img

--- --- --- img

这是我在设置中的静态配置:

This is my static configuration in settings:

STATIC_ROOT = '/Users/myuser/myprojectname/myprojectname'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    #normpath(join(SITE_ROOT, 'static')),
    os.path.join(BASE_DIR, "static"),
    '/Users/myuser/myprojectname/myprojectname/static',
)

这是显示的内容:

This is what it shows:

我已经做过一个集电体,这是行不通的。

I have already done a collectstatic and this doesn't work.

推荐答案

静态文件在Django中可能会造成混淆。我将尝试尽可能简单地解释...

Static files can be confusing in Django. I'll try to explain as simply as possible...

STATIC_ROOT
这是目录您应该在 生产 中提供静态文件。

STATIC_ROOT This is the directory that you should serve static files from in production.

STATICFILES_DIRS
这是您应在 开发 中提供静态文件的目录。

STATICFILES_DIRS This is the directory that you should serve static files from in development.

STATIC_ROOT和STATICFILES_DIRS不能指向同一目录。

STATIC_ROOT and STATICFILES_DIRS cannot point to the same directory.

Django是一个高度模块化的框架。一些应用程序模块包含自己的模板,css,图像和JavaScript。 Django管理员就是这样一个应用程序。 Django将这种模块化扩展到通过在开发和生产中使用不同目录的静态文件目录创建的应用程序。

Django is a highly modular framework. Some application modules contain their own templates, css, images and JavaScript. Django admin is one such app. Django extends this modularity to applications you create by using different directories for static files in development versus production.

DEBUG = True ,并且您在 INSTALLED_APPS 中包含了 django.core.staticfiles ,Django将提供 STATICFILES_DIRS 元组,使用 STATIC_URL 路径作为起点。

When DEBUG = True and you have included django.core.staticfiles in your INSTALLED_APPS, Django will serve the files located in the STATICFILES_DIRS tuple using the STATIC_URL path as the starting point.

在生产中,应将此责任交给Nginx,Apache,CloudFront等。当 DEBUG = False 时,Django将不会自动提供任何静态文件。

In production this responsibility should be given to Nginx, Apache, CloudFront, etc. When DEBUG = False, Django will not automatically serve any static files.

运行时:

$ python manage.py collectstatic

将STATICFILES_DIRS中指定的文件复制到STATIC_ROOT中进行部署。

The files specified in the STATICFILES_DIRS are copied into the STATIC_ROOT to be deployed.

因此,为回答您的问题,我将执行以下操作:

So, to answer your question, I would do the following:

创建另一个目录以在开发中存储您的静态文件并添加您的STATICFILES_DIRS的路径。我通常将此文件夹称为静态资产。它可以位于与现有静态目录相同的级别。

Create another directory to store your static files in development and add that path to your STATICFILES_DIRS. I usually call this folder "static-assets". It can reside at the same level as your existing "static" directory.

将STATIC_ROOT设置为现有静态目录的路径。

Set STATIC_ROOT to the path to your existing "static" directory.

如果您仔细查看屏幕快照中返回404的路径,则图像路径指定为:/static/img/logo.png,但是图像的目录为:/ static / image /

If you look closely at the path that's returning a 404 in your screenshot, the image path is specified as: /static/img/logo.png, but your directory for images is: /static/image/

因此,请仔细检查图像路径,以确保指向正确的目录。

So double-check your image path to make sure you're pointing to the right directory.

这篇关于如何正确指向Django中的静态图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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