网上自强学堂参考的
views.py
def home(request): info_dict = {'site': u'自强学堂', 'content': u'各种IT技术教程'} return render(request, 'home.html', {'info_dict': info_dict})
home.html
站点:{{ info_dict.site }} 内容:{{ info_dict.content }}
在模板中取字典的键是用点info_dict.site,而不是Python中的 info_dict['site'],效果如下:
还可以这样遍历字典:
{% for key, value in info_dict.items %} {{ key }}: {{ value }} {% endfor %}
其实就是遍历这样一个 List: [('site', u'自强学堂'), ('content', u'各种IT技术教程')]
下面是我自己写的
default.html
<div class="m-theme257"> <div class="m-theme257-al" data-scroll-reveal="enter bottom over 1s"> <div class="m-theme257-img"> <a href={{ 0.jumplink }} target="_blank"> <img class='lazyload' data-original={%static "postcard/priture/{{ 0.linkimage }}"%} alt="山区孩子拍摄的精美明信片)"> </a> </div> <div class="m-theme257-content"> <div class="m-theme257-title"> <a href={{ 0.jumplink }} target="_blank">{{ 0.name }}</a> </div> <div class="m-theme257-txt"> <a > 价格:{{ 0.price }}元 </a> </div> <div class="m-theme257-time">拍摄日期:{{ 0.takepicturetime }}</div> </div> </div>
view.html
def index(request): #get post cards message b = postcard.objects.all()[:9] #下面的语句返回前面5 个对象(LIMIT 5): a = len(b) c = {} d = {'displaytwo':'none','display':'block'} for i in range(0,a,1): list = {'name':b[i].name,'takepicturetime':b[i].takepicturetime,'linkimage':b[i].linkimage,'price':b[i].price,'jumplink':b[i].jumplink,} c[i]=list c['d']=d return render(request,'one/default.html',c)