zoukankan      html  css  js  c++  java
  • django: db

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

    一,修改 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>
  • 相关阅读:
    第二阶段个人冲刺总结01
    软件工程学习进度表13
    软件工程学习进度表12
    个人博客(09)
    个人博客(07)
    个人博客(08)
    poj1562 DFS入门
    poj3278 BFS入门
    数组单步运算
    十天冲刺
  • 原文地址:https://www.cnblogs.com/exclm/p/3366984.html
Copyright © 2011-2022 走看看