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

查看:47
本文介绍了如何在 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?

这是我的目录结构:

我的项目名称(顶级)

---我的项目名称

--- --- myproectname

--- --- myproectname

--- --- --- 设置

--- --- --- settings

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

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

--- --- 静态

--- --- --- 图像

--- --- --- 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',
)

它显示的是:

我已经做了一个 collectstatic 并且这不起作用.

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这是您应该在 development 中提供静态文件的目录.

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 admin 就是这样一款应用.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 将提供位于 INSTALLED_APPS 中的文件code>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天全站免登陆