数据库操作

查看数据表定义

[root@host-100-100-5-17 alu02]# cat blog/models.py
from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length = 20)
[root@host-100-100-5-17 alu02]# 

 

数据插入及查询

from blog.models import Employee
from django.shortcuts import render_to_response

def index(req):
    emps = Employee.objects.all()
    if not emps:
        emp = Employee()
        emp.name = 'alu02'
        emp.save()
        emps = Employee.objects.all()

    return render_to_response('index.html', {'emps':emps})

 

 

定义blog/templtes/index.html文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>{{title}}</title>
</head>
<body>
{% for emp in emps %}
<h1>{{emp.name}}</h1>
{% endfor %}
</body>
</html>

 

运行server

[root@host-100-100-5-17 alu02]# python manage.py runserver

 

测试界面

posted on 2016-03-07 23:36  onmyway227  阅读(115)  评论(0编辑  收藏  举报

导航