图像没有在Django runserver上以HTML格式呈现,但HTML在浏览器中工作 [英] Images not rendering in HTML on Django runserver, but HTML works in browser

查看:120
本文介绍了图像没有在Django runserver上以HTML格式呈现,但HTML在浏览器中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Django构建网站并且我的index.html页面无法正确呈现图像。当我在浏览器中打开HTML文件时,图像加载正常,但是当我从powershell执行 python manage.py runserver 时,HTML页面不会呈现任何图像。

Building a site with Django and my index.html page is not rendering images properly. The image loads up fine when I open the HTML file in the browser, but when I do python manage.py runserverfrom powershell the HTML page doesn't render any of the images.

代码如下 - 为什么在服务器上运行时不呈现,但在浏览器中打开时有效?

The code is below-- why is it not rendering when running in the server but works when I open in browser?

<!DOCTYPE html>
<head>Q&A</head>
<body>
<h1><img src="c:/users/chris/desktop/logo.jpg"></img></h1>
</body>


推荐答案

Django需要知道静态内容的位置服务他们。
执行以下操作:

Django needs to know the location of the static content to server them. Do the following:


  1. 将settings / STATIC_DIR的路径设置为存储图像的位置

  2. 提供一个看似客户端的STATIC_URL

  3. 将context_instance从您的视图代码传递到模板

  4. 使用网址在模板中{{STATIC_URL}} path_to_your_image_from_static_dir

  1. Set the path for settings/STATIC_DIR to the place where you are storing your images
  2. Provide a STATIC_URL which would appear to client
  3. Pass the context_instance from your view code to the template
  4. Use the url in the template {{STATIC_URL}}path_to_your_image_from_static_dir

这篇关于图像没有在Django runserver上以HTML格式呈现,但HTML在浏览器中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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