[英]How can I display an image on a web page in django that is on my desktop?
I am loading data from postgresSQL database.我正在从 postgresSQL 数据库加载数据。 This data is stored in the database by my python program.该数据由我的 python 程序存储在数据库中。 Not I am fetching data with the same functions I am using in my program.不是我正在使用我在程序中使用的相同功能获取数据。 I want to create a bar chart before displaying it on my HTML webpage in Django.我想在 Django 的 HTML 网页上显示之前创建一个条形图。 I created a chart using matplotlib and saved it on the desktop.我使用 matplotlib 创建了一个图表并将其保存在桌面上。 Now I want to fetch that image and display it.现在我想获取该图像并显示它。 I tried giving a link directly to that image but that doesn't load the picture.我尝试直接提供指向该图像的链接,但不会加载图片。
According to the Django documentation :根据Django 文档:
In your settings file, define STATIC_URL
, for example:在您的设置文件中,定义STATIC_URL
,例如:
STATIC_URL = '/static/'
In your templates, use the static template tag to build the URL:在您的模板中,使用 static 模板标签来构建 URL:
{% load static %} <img src="{% static "example.jpg" %}" alt="My image">
Store your example.jpg
image file in a folder called static
in your app.将您的example.jpg
图像文件存储在应用程序中名为static
的文件夹中。
You can directly render the image in your python function with matplotlib.您可以使用 matplotlib 直接在 python function 中渲染图像。 After that, you will store the image to your server.之后,您将图像存储到您的服务器。 You can then can get the link and pass it to the django template.然后,您可以获得链接并将其传递给 django 模板。 Just render the img tag with the link as the src attribute.只需将带有链接的 img 标签作为 src 属性呈现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.