Django模板img src不起作用 [英] Django template img src not working

查看:426
本文介绍了Django模板img src不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在Django模板文件 base.html中使用img src标签来打印图像:

I want to print an image by using a img src tag in a Django template file "base.html":

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Foto</title>
</head>
<body>
    <h1>My helpful timestamp site</h1>
    <img src="google.png" / >
    <hr>
    <p>Made by ... </p>
</body>
</html>

在views.py中,我定义:

In views.py I define:

def hello(request):
    return render_to_response('base.html')

但是图像未显示在浏览器中。如果我将其作为简单的html文件打开,它将显示在浏览器中。

But the image does not show up in the browser. If I open it as a simple html file, it shows up in the browser.

推荐答案

发生这种情况是因为Django不知道此图像的路径。

创建一个名为 static / 的文件夹,然后创建一个名为 images / 在项目根目录(您的settings.py文件所在的位置)中。

That happens because Django does not know the path to this image.
Make a folder named static/, and then a folder named images/ in your project root(where your settings.py file resides).

my_project/
    my_project/ 
        settings.py
        static/
           images/
             google.png

然后将其更改为:

<img src="{{STATIC_URL}}images/google.png" / >

更多此处

这篇关于Django模板img src不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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