![](/img/trans.png)
[英]jquery from base.html not working in other templates in django
[英]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>© 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.