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

一,修改 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>

django: db - display的更多相关文章

  1. 在django项目外,使用django.db链接数据库(postgres)

    要用python链接到数据库,又不想写太多代码.想到了django,就偷懒了下.用django.db直连. django版本:1.6.5 (1.5以后可以用以下代码) #coding=utf-8 __ ...

  2. django - from django.db.models import F - class F

    F() 的执行不经过 python解释器,不经过本机内存,是生成 SQL语句的执行. # Tintin filed a news story! reporter = Reporters.objects ...

  3. Django db relationship

    # coding=utf-8 from django.db import models """ Django数据库关系: 一对一关系:OneToOneField 多对多关 ...

  4. django: db - admin

    本讲演示简单使用 Django Admin 功能. 一,修改 settings.py,添加 admin 应用: INSTALLED_APPS = ( 'django.contrib.auth', 'd ...

  5. django: db - many to one

    models 模块中的对象有三种对应关系:多对一,多对多,一对一.本讲说明多对一关系. blog/models.py: from django.db import models class Emplo ...

  6. django: db howto - 2

    继 django: db howto - 1 : 一 操作数据库的三种方式: [root@bogon csvt03]# python2.7 manage.py shell Python 2.7.5 ( ...

  7. django: db howto - 1

    以在 Django 中使用 MySQL 为例,首先要安装 MySQL 和 MySQL-python 组件,确保 python 能执行 import MySQLdb. MySQL 中创建数据库: [ro ...

  8. django.db.utils.OperationalError: 1050解决方案

    manage.py migrate时进行同步数据库时出现问题;django.db.utils.OperationalError: (1050, "Table '表名' already exi ...

  9. 报错django.db.migrations.exceptions.InconsistentMigrationHistory

    Pycharm强大的功能总是让我很是着迷,比如它的makemigrations 和 migrate. 然而某一次,当我再次敲下这熟悉的命令时,它报错了.... Traceback (most rece ...

随机推荐

  1. CIPAddressCtrl控件

    CIPAddressCtrl m_ipCtrl; 1.获取控件IP值 int GetAddress(byte& byteFirst, byte& byteTwo, byte& ...

  2. 【C#编程基础学习笔记】6---变量的命名

    2013/7/24 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com [C#编程基础学习笔记]6---变量的命名 ----- ...

  3. centos 安装php

    1.yum安装 yum install php php-fpm php-common php-gd php-mcrypt php-pear php-pecl-memcache php-mhash ph ...

  4. Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结【转载】

    详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON() 一,$.get(url,[data],[callback]) 说明:url为请求地址,d ...

  5. js 邮箱正则匹配

    $(function(){ var valAttr = '746979855qq.com'; \-])+\.)+([a-zA-Z0-]{,})+$/; var tmpresult = mail_fil ...

  6. dede去除powered by dedecms

    include/dedesql.classs.php文件中找到第588行: $arrs1 = array(0x63,0x66,0x67,0x5f,0x70,0x6f,0x77,0x65,0x72,0x ...

  7. Hibernate中get方法和load方法的区别

    一.get和load方法都是根据id去获得对应数据的,但是获得机制不同:如果使用get方法,hibernate会去确认该id对应的数据是否存在,它首先会去session中去查询(session缓存其实 ...

  8. cocos2d(粒子效果编辑器)

    ParticleDesigner  for  Mac下载地址:http://www.cocoachina.com/bbs/read.php?tid=108339 最近在做一款粒子编辑器 其实就是在co ...

  9. IOS面试攻略

    IOS面试攻略(1.0) 2013-10-13 20:58:09|  分类: IOS面试 |  标签:ios知识点总汇  ios面试  |举报|字号 订阅     来自:伊甸网 @ 看到这个关键字,我 ...

  10. runtime 运行时机制 完全解读

    runtime 运行时机制 完全解读   目录[-] import import 我们前面已经讲过一篇runtime 原理,现在这篇文章主要介绍的是runtime是什么以及怎么用!希望对读者有所帮助! ...