django: db - display
本讲介绍数据在页面中的呈现,内容很简单,就是嵌套循环在模板中的使用。
一,修改 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的更多相关文章
- 在django项目外,使用django.db链接数据库(postgres)
要用python链接到数据库,又不想写太多代码.想到了django,就偷懒了下.用django.db直连. django版本:1.6.5 (1.5以后可以用以下代码) #coding=utf-8 __ ...
- django - from django.db.models import F - class F
F() 的执行不经过 python解释器,不经过本机内存,是生成 SQL语句的执行. # Tintin filed a news story! reporter = Reporters.objects ...
- Django db relationship
# coding=utf-8 from django.db import models """ Django数据库关系: 一对一关系:OneToOneField 多对多关 ...
- django: db - admin
本讲演示简单使用 Django Admin 功能. 一,修改 settings.py,添加 admin 应用: INSTALLED_APPS = ( 'django.contrib.auth', 'd ...
- django: db - many to one
models 模块中的对象有三种对应关系:多对一,多对多,一对一.本讲说明多对一关系. blog/models.py: from django.db import models class Emplo ...
- django: db howto - 2
继 django: db howto - 1 : 一 操作数据库的三种方式: [root@bogon csvt03]# python2.7 manage.py shell Python 2.7.5 ( ...
- django: db howto - 1
以在 Django 中使用 MySQL 为例,首先要安装 MySQL 和 MySQL-python 组件,确保 python 能执行 import MySQLdb. MySQL 中创建数据库: [ro ...
- django.db.utils.OperationalError: 1050解决方案
manage.py migrate时进行同步数据库时出现问题;django.db.utils.OperationalError: (1050, "Table '表名' already exi ...
- 报错django.db.migrations.exceptions.InconsistentMigrationHistory
Pycharm强大的功能总是让我很是着迷,比如它的makemigrations 和 migrate. 然而某一次,当我再次敲下这熟悉的命令时,它报错了.... Traceback (most rece ...
随机推荐
- TreeView 数据绑定及选中命令处理
昨天接近下班,一个群里面的网友,问treeView绑定后 选中命令怎么来处理,怎么没有效果,而且用MVVM的方式来写:快下班了本来想远程帮他看下,结果就说写个Demo给他看:再加上选中传参: 下面分 ...
- Java下载Servlet Demo
request.setCharacterEncoding("utf-8"); String name=request.getParameter("name"); ...
- Linux定时任务命令crontab
作为一个php码农,linux接触总是很少,但又是必须会的一项技能,今天学到的linux命令是crontab命令. 注:以下总结基于Ubuntu 12.04 去/etc目录下看看 先去Ubuntu的/ ...
- Day02
1.os.system()和os.popen() os.popen() 功能强于os.system() , os.popen() 可以返回回显的内容,以文件描述符返回 eg: import os re ...
- OpenStack JEOS 镜像
JEOS:Just Enough Operating System 维基百科地址:http://en.wikipedia.org/wiki/Just_enough_operating_system O ...
- nginx读书日志
2016.12.20 nginx 功能特性: 既可以作为http服务器,也可以作为反向代理服务器或者邮件服务器 基本服务:处理静态文件,处理索引文件以及支持自动索引 nginx服务器中主要由proxy ...
- 提取所有mtk机型的线刷包
首先手机root且安装busybox和超级终端软件,这里不提供自己百度一大堆 一.制作scatter 示范 PRELOADER 0x0 { } MBR 0x600000 { } EBR1 0x6800 ...
- z-index的最大值、最小值
浏览器 最大值 超过最大值后变成 最小值 小于最小值后变成 备注 IE6 2147483647 2147483647 -2147483648 -2147483648 IE7 2147483647 ...
- Baidu Map Web API 案例
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- PHP Java
http://my.oschina.net/lajp/blog/5121 http://blog.163.com/lijianwei_123/blog/static/18489289120115244 ...