cstar

eli's docs

   :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

本讲介绍数据在页面中的呈现,内容很简单,就是嵌套循环在模板中的使用。

一,修改 csvt03/urls.py:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

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

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(r'^index/$','blog.views.index'),
    url(r'^blog/show_author/$', 'blog.views.show_author'),
)

二,定制视图处理函数 blog/views.py:

from django.shortcuts import render_to_response as r2r
from blog.models import Employee, Author, Book

def index(req):
    emps = Employee.objects.all()
    return r2r('index.html', {'emps':emps})

def show_author(req):
    authors = Author.objects.all()
    return r2r('show_author.html', {'authors':authors})

三,定制模板文件 blog/templates/show_author.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Django DB</title>
    </head>
    <body>
        {% for author in authors %}
        <h3>{{author.name}}</h3>
        <div>
            {% for book in author.book_set.all %}
            {{ book }}
            {% endfor %}
        </div>
        {% endfor %}
    </body>
</html>

 

posted on 2013-10-13 20:09  exclm  阅读(366)  评论(0编辑  收藏  举报