django之创建第8-3个项目-数据库数据提取之高级操作

1、配置test2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<li>获取单个数据</li>
<li>ID:{{student.id}}---name:{{student.name}}---sex:{{student.sex}}</li>

</body>
</html>

2、配置student_list。html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<li>遍历数据库student中的数据</li>
{% for student in studentList %}
    <li>{{student}}</li>  <!--Student objec-->
{% endfor %}

<br>
<li>数据过滤</li>
{% for student in studentList %}
    <li>{{student.name}},{{student.age}}</li>
{% endfor %}

<li>获取单个数据</li>
<li>{{student}}</li>

</body>
</html>

3、配置urls文件

# -*- coding: UTF-8 -*-
from django.conf.urls import patterns, include, url


urlpatterns = patterns('blog.views',
    url(r'^index/$', 'index'),
    url(r'^time/$', 'time'),
    url(r'^shengfen/$', 'shengfen'),
    #url(r'^foo/(\d{4})/$', 'foo'), #表示接收4个数字,#\w+接收至少一个字母
    url(r'^foo/(\d{4})/(\w+)/$',"foo"),
    url(r'^bar/(?P<id>\d{4})/(?P<name>\w+)/$','bar'),#强制规定bar(request,id,name)处的参数必须为id和name
    url(r'^student_list/$','student_list'),
    url(r'^test2/$','test2'),
)

4、配置views文件

# Create your views here.
#coding:utf-8
from django.http import HttpResponse
import datetime
#导入templates文件所需导入库
from django.template import loader,Context
#引入Student等模块
from blog.models import *


def student_list(request):
    t=loader.get_template("student_list.html")

    #第1种方法:输出数据库中student表中的数据
    #studentList = Student.objects.all()  # 等价于 select * from student

    #第2种方法:根据age字段做排序处理并输出结果  .order_by("age")
    #studentList = Student.objects.all().order_by("age")

    #第3种方法:根据age字段做降序处理并输出结果  .order_by("-age")
    #studentList = Student.objects.all().order_by("-age")

    # 第4种方法:支持多个字段排序
    #studentList = Student.objects.all().order_by("age","name")

    # 第5种方法:支持过滤
    #studentList = Student.objects.filter( age= 16 )

    # 第6种方法:过滤大于16的数据
    #age__gt=16等价于age > 16,注意age和__gt之间是没有空格的
    studentList = Student.objects.filter(age__gte = 16)

    c = Context({"studentList": studentList})
    return HttpResponse(t.render(c))


def test2(request):
    t=loader.get_template("test2.html")

    #第7种方法:获取单个数据
    student = Student.objects.get(id=1)

    #第8种方法:修改数据库数据并保存
    student.name='xiaodeng'
    student.age=28
    student.intime = '1987-08-22'
    student.save()

    #第9种方法:批量更新数据
    studentList=Student.objects.filter(age__lt=17).update(name = 'FengMei')

    #新增语句,刷新多少次就会增加多少条记录
    newData=Student(name='python',age=1000,intime='1999-09-09',sex=0)
    newData.save()

    #删除数据,二次操作会报错,因为索引7已经删除了,不存在了,再次删除索引7当然会出错。
    #student = Student.objects.get(id=7)
    #student.delete()


    # 删除所有数据
    #Student.objects.all().delete()
    #student={}

    student = Student.objects.get(id=1)
    c = Context({"student": student})
    return HttpResponse(t.render(c))


class Person():
    def __init__(self,name,age,sex):
        self.age=age
        self.sex=sex
        self.name=name

    def myMethod(self):
        return "get it"

def time(request):
    t = loader.get_template("time.html")
    myId = request.GET.get("id")
    myName = request.GET.get("name")#获取传递的name的value值
    user = {"today": datetime.datetime.now(),"id":myId,"name":myName}
    c = Context(user)
    return HttpResponse(t.render(c))

def foo(request,myID,myName):
    t = loader.get_template("foo.html")
    user = {"today": datetime.datetime.now(),"id":myID,"name":myName}
    c = Context(user)
    return HttpResponse(t.render(c))

def bar(request,id,name):
    t = loader.get_template("bar.html")
    user = {"today": datetime.datetime.now(),"id":id,"name":name}
    c = Context(user)
    return HttpResponse(t.render(c))

def shengfen(request):
    t = loader.get_template("shengfen.html")
    user = {"shengfen":"湖北省恩施市"}
    c = Context(user)
    return HttpResponse(t.render(c))

def index(request):
    #第二个项目
    #return HttpResponse("hello,Django")

    #加载器,加载模板
    t=loader.get_template("index.html")

    # django之创建第4-1个项目-Dict形式
    user = {"name": "xiaodeng", "sex": "male", "age": 28,"today":datetime.datetime.now()}
    c = Context({"test": user})  # 在这里test位变量,user为变量的值
    return HttpResponse(t.render(c))

 

5、百度云盘:django之创建第8-3个项目-数据库数据提取之高级操作

posted @ 2016-04-05 16:40  Xiao|Deng  阅读(467)  评论(0编辑  收藏  举报