码迷,mamicode.com
首页 > 数据库 > 详细

数据库操作

时间:2016-03-08 00:22:13      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

查看数据表定义

[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

 

测试界面

技术分享

数据库操作

标签:

原文地址:http://www.cnblogs.com/onmyway227/p/5252455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!