django+mysql+html简单demo之 views+html
2024-09-03 16:31:25
#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属性表示返回到页面地址后边的调用函数,就是页面跳转的意思。
最新文章
- 分享一种容易理解的js去重排序方法
- SCI英文论文写作- Latex 进阶
- <;实训|第十一天>;学习一下linux中的进程,文件查找,文件压缩与IO重定向
- jquery datatable
- 做办公用品、文具方面的 B2C 是否有前景呢?
- cmd.ExecuteReader(CommandBehavior.CloseConnection)
- SSRS 迁移
- 【NYOJ-187】快速查找素数—— 枚举法、筛选法、打表法
- JQuery: 微博演示
- Qt之OpenSSL(有pro文件的路径格式)
- 一个请求中,ADF、JSF究竟做了哪些工作
- 免费翻译API破解(简易翻译工具)
- Asp.Net MVC 利用ReflectedActionDescriptor判断Action返回类型
- manjaro折腾手记
- .14-浅析webpack源码之Watchpack模块
- A1054. The Dominant Color
- VS-常用的快捷键-总结
- 清理configure脚本生成的文件
- 如何写一个优秀的GitHub项目README文档?
- Final阶段贡献分配规则及实施