django 显示静态文件的几种方式

本文介绍了在Django中如何显示静态文件和媒体文件的几种方法,包括使用tag、直接写全路径和通过配置settings.py。针对媒体文件的显示问题,详细解释了错误的原因和解决方案,涉及MEDIA_URL的设置以及urls.py的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个问题还整了半天,问了兄弟波仔,终于解决!

先看效果!

代码如下:

[root@localhost templates]# more base.html 
{% load staticfiles %}
<html>
<head>
    <title>{% block title %}{% endblock %}</title>
    <style type="text/css">
    #body{color:#efd;background:#000000;padding:0 5em;margin:0}
    body{color:#efd;background:url("{% static "images/background.gif" %}");padding:0 5em;margin:0}
    #body {background: white url("images/background.gif") no-repeat right bottom;}
    h1{padding:2em 1em}
    #h1{padding:2em 1em;background:#080000}
    h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em}
    p{margin:1em 0;color:#080067}
    </style>
</head>
<body>
    {% load static %}
    <a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
    <a href="http://www.baidu.com"><img src="{{STATIC_URL}}images/sitelogo.png"  alt="static_url type" /></a>
    <a href="http://www.baidu.com"><img src="/jingtai/images/sitelogo.png"  alt="abs type" /></a>
    {% block content %}{% endblock %}
</body>
</html>
[root@localhost templates]# pwd
/root/Desktop/data/download/django/mysite16_5_demo4/news/templates
[root@localhost templates]# 

显示效果如下



先看第一种显示方式:

<a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>

使用tag的形式

普通配置即可,如下:

[root@localhost mysite16_5_demo]# pwd
/root/Desktop/data/download/django/mysite16_5_demo4/mysite16_5_demo
[root@localhost mysite16_5_demo]# tail -10 settings.py


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

MEDIA_ROOT = '/headImg'
MEDIA_URL = '/data/'
STATIC_ROOT='/54Static' 
#STATIC_URL = '/static/'
STATIC_URL='/jingtai/'
[root@localhost mysite16_5_demo]# 

html解析如下




再看第三种显示方式,直接写全路径!setting.py配置同上!缺点是不易于维护


主要是第二种显示方式

setting配置还是同上,但是图片总是显示不出来!

显示效果如下:


修改显示的方法

#year
def year_archive(request, year):
    a_list = Article.objects.filter(pub_date__year=year)
    return render_to_response('year_archive.html', {'year': year, 'article_list': a_list},RequestContext(request))

return render_to_response('year_archive.html', {'year': year, 'article_list': a_list})

改为

return render_to_response('year_archive.html', {'year': year, 'article_list': a_list},RequestContext(request))


增加

RequestContext(request)

记得引用

from django.template import RequestContext




---add 2015年8月13日 17:57:45

显示静态文件


urls.py配置如下


[root@localhost mysite16_5_demo]# more urls.py
from django.conf.urls import patterns, include, url
import settings
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite16_5_demo.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    #(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': 'media'}), 
    (r'^articles/(\d{4})/$', 'news.views.year_archive'),
    url(r'^news/', include('news.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^comm/',include('comm.urls')),

)
if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT }),
        url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT}),
        )
[root@localhost mysite16_5_demo]# 



add 2015年8月14日 01:00:08 

如果我写的路径不是static,而是media呢,我该怎么显示

按照之前的思路,我应该这样改就可以了呀

    <a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
    <a href="http://www.baidu.com"><img src="{{MEDIA_URL}}upload/5.jpg"  alt="static_url type" /></a>
    <a href="http://www.baidu.com"><img src="/data/upload/5.jpg"  alt="abs type" /></a>

但是除了第一个,其他的都没有正确显示,难道哪里错了,按照逻辑来说的话应该没变呀

后台打印的是

[12/Aug/2015 23:16:28] "GET /data/upload/5.jpg HTTP/1.1" 404 2533


难道是找不到这个路路径,将settings.py 中的MEDIA_URL='/media/'时

同事修改模版为

    <a href="http://www.baidu.com"><img src="{% static "images/sitelogo.png" %}" alt="static type" /></a>
    <a href="http://www.baidu.com"><img src="{{MEDIA_URL}}upload/5.jpg"  alt="static_url type" /></a>
    <a href="http://www.baidu.com"><img src="/media/upload/5.jpg"  alt="abs type" /></a>

图片正常显示

其实原因为我urls.py的配置


后台打印为

[12/Aug/2015 23:21:36] "GET /news/articles/2014/ HTTP/1.1" 200 14799
[12/Aug/2015 23:21:36] "GET /jingtai/images/sitelogo.png HTTP/1.1" 304 0
[12/Aug/2015 23:21:36] "GET /jingtai/images/background.gif HTTP/1.1" 304 0
[12/Aug/2015 23:21:36] "GET /media/upload/5.jpg HTTP/1.1" 304 0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值