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>
  • 相关阅读:
    BZOJ 2212/BZOJ 3702
    BZOJ 4761 Cow Navigation
    BZOJ 3209 花神的数论题
    BZOJ 4760 Hoof, Paper, Scissors
    BZOJ 3620 似乎在梦中见过的样子
    BZOJ 3940 Censoring
    BZOJ 3942 Censoring
    BZOJ 3571 画框
    BZOJ 1937 最小生成树
    BZOJ 1058 报表统计
  • 原文地址:https://www.cnblogs.com/exclm/p/3366984.html
Copyright © 2011-2022 走看看