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>
  • 相关阅读:
    从一个表中查数据,插入另一个表
    sql 字段字符串内容替换
    安装VS2010后,如何设置老版本的项目文件不是默认用VS2010打开
    题解 [JOI 2019 Final] 独特的城市
    题解 [JOI 2019 Final] 硬币收藏
    题解 [CF720A] Closing ceremony
    [学习笔记] Miller-Rabin 质数测试
    题解 [CF332C] Students' Revenge
    题解 [CF525D] Arthur and Walls
    SpringMVC-拦截器
  • 原文地址:https://www.cnblogs.com/exclm/p/3366984.html
Copyright © 2011-2022 走看看