简体   繁体   中英

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

I have a base.html which i extends to other pages also. In few pages , images are displayed but in few it does not. other than images , everything like header , section are displayed.

{% 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>

My template file

{% 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 %}

url ` from django.conf.urls import url from . import views

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'),

]` views

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)

You're loading staticfiles but you never actually use it, you should use the static template tag

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

should be

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

You should also use the url template tag..

It's because you're not using the correct src. You should let the static function handle the static files. When the url changes ../../ will not be correct anymore, depending on the path.

You should configure the static directory in your settings.py file and then reference your image like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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