0

我的设置文件如下所示,

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static', 'static_dirs'),)

MEDIA_URL = '/static/images/'
MEDIA_ROOT = '/Users/bhargavsaidama/5ai/source/static/images/'

#MEDIA_ROOT = os.path.join(BASE_DIR,'static', 'images') (tried this too)

我的html加载页面如下,注意:我这里直接使用文件路径

索引.html:

<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

我的实际 html 文件是:

{% extends "index.html" %}


{% block content %}

<div class="content">

    <img src ='/Users/bhargavsaidama/5ai/source/5ai/static/images/Indian-economy.jpg' alt="My image">

    <h2 id = "title"><font face = "Comic sans MS"> {{ post.title }} </font></h2>

    {% for sub_text in post.content %}

    <p id = "data"><font face = "Comic sans MS" size="+0.3"> {{ sub_text }} </font></p>

    {% endfor %}

</div>
{% endblock %}

即使我尝试使用:

<img src ='Indian-economy.jpg' alt="My image"> 

....但没有运气

但输出是 在此处输入图像描述

如果我尝试使用普通的 html 文件,可以说:

<html>
<p>  this is bhargav sai</p> 
<img src= '/Users/bhargavsaidama/5ai/source/5ai/static/images/Indian-economy.jpg' alt = 'my image'>
</html>

输出是: 在此处输入图像描述

甚至我来自本地主机的直接 url 也能够获取图像: 在此处输入图像描述

谁可以帮我这个事?

4

2 回答 2

1

你最好知道如何配置和加载静态文件,参考官方文档。

由于您jpgstatic/images文件夹中,因此只需像这样更改您的实际 html:

{% extends "index.html" %}


{% block content %}

{% load static %} #load static directory
<div class="content">

    #load your static image
    <img src ='{% static "images/Indian-economy.jpg" %}' alt="My image">

    <h2 id = "title"><font face = "Comic sans MS"> {{ post.title }} </font></h2>

    {% for sub_text in post.content %}

    <p id = "data"><font face = "Comic sans MS" size="+0.3"> {{ sub_text }} </font></p>

    {% endfor %}

</div>
{% endblock %}

这将适用于您的情况。

于 2017-05-14T13:38:43.753 回答
0

如果您来自 Google,请注意您的设置文件中的调试状态,访问开发服务器和产品服务器中的媒体文件有点不同。在产品服务器中,它们必须使用您的 Web 服务器(apache/nginx)进行配置。请查看官方文档

于 2017-05-29T01:21:43.787 回答