#coding=utf-8
from __future__ import unicode_literals from django.shortcuts import render,render_to_response
from django.http import HttpResponse from django.template import loader
from books import models # Create your views here. def book_page(request):
#取出指定字段所有值
#name_list=models.Publisher.objects.values('name',"city")
#get查找,只能查询一条数据
#name_list=models.Publisher.objects.get(id=1) #修改数据,改完再获取
#models.Publisher.objects.filter(id=1).update(city="chengdu")
#name_list=models.Publisher.objects.get(id=1) #修改数据,获取后再改。
#name_list=models.Publisher.objects.get(id=1)
#name_list.city="guangzhou"
#name_list.save() #删除数据,找不到,会抛出 Publisher matching query does not exist。
models.Publisher.objects.filter(id=2).delete()
name_list=models.Publisher.objects.get(id=2) #name_list=models.Publisher.objects.all()
#name_list=[{'name':'zhangsan','city':'beijing'},{'name':'lisi','city':'shanghai'}]
return render_to_response('show.html',{'name_list':name_list}) def search_form(request):
return render_to_response('form.html',{}) def search(request):
if 'name' in request.GET and request.GET['name']:
name=request.GET['name']
books=models.Book.objects.filter(title__icontains=name)
return render_to_response('search.html',{'books':books,'query':name})
else:
return render_to_response('form.html',{'error':True}) def search_form1(request):
return render_to_response('form0.html',{}) def search1(request):
errors=[]
if 'name' in request.GET:
name=request.GET['name']
if not name:
errors.append("Enter a content.")
elif len(name)>20:
errors.append("Please enter less than 20 code.")
else:
books=models.Book.objects.filter(title__icontains=name)
return render_to_response('search.html',{'books':books,'query':name}) return render_to_response('form0.html',{'errors':errors})

views.py

 # -*- coding: utf-8 -*-
from __future__ import unicode_literals from django.db import models # Create your models here.
class Publisher(models.Model):
name = models.CharField(max_length = 30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
#__unicode__Õâžöº¯ÊýÓÃÀŽ·µ»ØijžöÖµ¿ÉÒԺܺõÄÓÃÓÚ²éѯºÍadminœçÃæµÄÏÔÊŸ
def __unicode__(self):
return self.name class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
emial = models.EmailField(blank=True,verbose_name = 'e-mail')
def __unicode__(self):
return u'%s %s'%(self.first_name,self.last_name) class Book(models.Model):
title = models.CharField(max_length = 100)
author = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField(blank = True,null = True)
def __unicode__(self):
return self.title

models.py

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>查询</title>
</head>
<body>
{% if error%}
<ul>
{% for error in errors%}
<li> {{error}}</li>
{%endfor%}
</ul>
{%endif%}
<form class="" action="/search/" method="get">
<input type="text" name="name" value="">
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>

form.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>查询</title>
</head>
<body>
{% if errors%}
<ul>
{% for error in errors%}
<li> {{error}}</li>
{%endfor%}
</ul>
{%endif%}
<form class="" action="/search1/" method="get">
<input type="text" name="name" value="">
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>

form0.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>result</title>
</head>
<body>
<p> search for: <strong>{{query}}</strong> </p>
{% if books%}
<p>
found {{books | length }} book {{books | pluralize}}
</p>
<ul>
{% for book in books%}
<li>{{book.title}}</li>
{%endfor%}
</ul>
{%else%}
<p>
No books matched your search.
</p>
{% endif%}
</body>
</html>

search.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>show</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<td> Name</td>
<td> City </td>
<td> address </td>
<td> state_province</td>
<td> country </td>
<td> website </td>
</tr>
</thead>
<tbody>
{#% for line in name_list%#}
<tr>
<td> {{line.name}} </td>
<td> {{line.city}} </td>
<td> {{line.address}} </td>
<td> {{line.state_province}} </td>
<td> {{line.country}} </td>
<td> {{line.website}} </td>
</tr>
{#% endfor%#}
<tr>
<td> {{name_list.name}} </td>
<td> {{name_list.city}} </td>
<td> {{name_list.address}} </td>
</tbody>
</table>
</body>
</html>

show.html

注意:html页面表单中的action属性表示返回到页面地址后边的调用函数,就是页面跳转的意思。

最新文章

  1. 分享一种容易理解的js去重排序方法
  2. SCI英文论文写作- Latex 进阶
  3. &lt;实训|第十一天&gt;学习一下linux中的进程,文件查找,文件压缩与IO重定向
  4. jquery datatable
  5. 做办公用品、文具方面的 B2C 是否有前景呢?
  6. cmd.ExecuteReader(CommandBehavior.CloseConnection)
  7. SSRS 迁移
  8. 【NYOJ-187】快速查找素数—— 枚举法、筛选法、打表法
  9. JQuery: 微博演示
  10. Qt之OpenSSL(有pro文件的路径格式)
  11. 一个请求中,ADF、JSF究竟做了哪些工作
  12. 免费翻译API破解(简易翻译工具)
  13. Asp.Net MVC 利用ReflectedActionDescriptor判断Action返回类型
  14. manjaro折腾手记
  15. .14-浅析webpack源码之Watchpack模块
  16. A1054. The Dominant Color
  17. VS-常用的快捷键-总结
  18. 清理configure脚本生成的文件
  19. 如何写一个优秀的GitHub项目README文档?
  20. Final阶段贡献分配规则及实施

热门文章

  1. handsontable-developer guide-cell type
  2. SQL Server 数据库的分类和用户数据库文件组成
  3. WPF自定义控件之列表滑动特效 PowerListBox
  4. Python脱产8期 Day15 2019/4/30
  5. 实时监测input输入变化 jQuery
  6. [JS] js 判断用户是否在浏览当前页面
  7. Nginx+SpringBoot搭建负载均衡
  8. Settings app简单学习记录
  9. python 爬恶魔法则(单线程卡成狗)
  10. 栈的理解和代码实现(java)