html - 图像不显示。 Django

标签 html django django-templates

找不到我做错了什么(我猜应该是语法错误)。我的照片没有显示。我得到“没有文件”。

html

    <!-- RECENTLY ADDED FILES -->
        <div class="buttom-box floatleft">
            <h3>Recently Added Files</h3>
                <p>New class papers. Now you can enjoy and study them</p>
                <br>
                {% if recent_files %}
                    {% for file in recent_files %}
                        <img src="{{ MEDIA_URL }}{{ file.image }}" alt="Sample 1" width="125" height="125" />
                    {% endfor %}
                {% else %}
                    <p>NO FILES</p>
                {% endif %}
        </div>

View .py

def index(request):  
    recent_files = FileDescription.objects.all()

    context_instance=RequestContext(request)
    return render_to_response('index.html', 
        {'recent_files':recent_files,},
        context_instance,
        )

MySQL for FileDescription:

mysql> SELECT * FROM school_filedescription;
+----+------------+--------------+-----------+------------------+-----------+------------------+---------------------+---------------------+-----------------------------+
| id | subject_id | subject_name | file_type | file_uploaded_by | file_name | file_description | file_creation_time  | file_modified_time  | image                       |
+----+------------+--------------+-----------+------------------+-----------+------------------+---------------------+---------------------+-----------------------------+
|  1 |          1 | Math 140     | class     | rrr              | lalala    | lalala           | 2012-08-23 12:12:12 | 2012-08-23 12:12:12 | files/rrr/class/smart.jpg   |
|  2 |          1 | Math 140     | class     | rrr              | ,s,s,s    | s msms           | 2012-08-23 12:32:39 | 2012-08-23 12:32:39 | files/rrr/class/smart_1.jpg |
+----+------------+--------------+-----------+------------------+-----------+------------------+---------------------+---------------------+-----------------------------+
2 rows in set (0.00 sec)

然而,其他页面中的这段代码工作正常

其他页面:

{% extends "base.html" %}
{% block main-menu %}
    <div class="contentarea">       
        <img src="{{ MEDIA_URL }}{{ picture.image }}"/>
    </div>
{% endblock %}

最佳答案

尝试将最近的文件参数添加到您的请求上下文实例中,如下所示:

context_instance = RequestContext(request, {
  'recent_files': recent_files
})

然后像这样调用请求...

return render_to_response('index.html', context_instance = context_instance)

关于html - 图像不显示。 Django ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097396/

相关文章:

django访问许多创建的原始表字段

html - 如何同时悬停两个元素?

html - 底部定位按钮重叠内容

html - CSS 格式化列

python - 如何在 Django 中只打印当前登录的用户数据?

html - 将值从 Django View 传递到 AngularJS 插入的模板

css - 使用 bootstrap 4 在移动设备中查看时如何隐藏文本链接

html - 我可以让 span 元素忽略列吗?

python - Django 保存点回滚捕获完整性错误导致 TransactionManagementError

python - Django 中测试断言的可重用助手