繁体   English   中英

在django的几页中未从base.html显示图像

[英]Image is not getting displayed from base.html in few pages in django

我有一个base.html,我也将其扩展到其他页面。 在很少的页面中,显示图像,但很少显示。 除了图像以外,还会显示诸如header,section之类的所有内容。

{% load staticfiles %}
some more --like header , section 
<footer>
<div id="footer">
    {% block footer %}
        <a href="https://github.com/shanker4999"> <img src ="../../static/blog/images/git.png"></a>
        <p>&copy; 2016 shankar.</p>
    {% endblock %}
</div>

我的模板文件

{% extends 'blog/base.html' %}


{% block content %}
<h1>Articles for {{ year }}</h1>

{% for article in article_list %}
<h4><a href="/blog/{{article.id}}/">{{ article.headline }}</a></h4>
<h5>Posted by <strong>{{ article.reporter }}</strong>
                 on {{article.pub_date|date:"F j, Y"}}</h5><hr/>
{% endfor %}
{% endblock %}

来自django.conf.urls的url`从。 导入视图

urlpatterns = [
    url(r'^$',views.index,name='index'),
    url(r'article/(?P<year>[0-9]{4})/$', views.year_archive, name='year_archive'),
    url(r'article/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive, name='month_archive'),
    url(r'(?P<article_id>[0-9]+)/$',views.article_detail,name='article_detail'),
    url(r'^comment/(?P<article_id>[0-9]+)/$' ,views.comment,name='comment'),
    url(r'^contact',views.contact,name='contact'),

]`视图

ef year_archive(request,year):
#year=str(pub_date)[0:4]
year=year
try:
    article_list = Article.objects.filter(pub_date__year=year)
except Article.DoesNotExist:
    raise Http404("Article does not Exists")
context = {'year':year, 'article_list':article_list}
return render(request, 'blog/year_archive.html',context)

您正在加载staticfiles但实际上从未使用过,应该使用static template标签

"../../static/blog/images/git.png"

应该

{% static 'blog/images/git.png' %}

您还应该使用url模板标记。

这是因为您没有使用正确的src。 您应该让静态函数处理静态文件。 当URL变化../../将不再正确,取决于路径上。

您应该在settings.py文件中配置静态目录,然后像这样引用您的映像:

<img src ="{% static 'blog/images/git.png' %}"></a>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM