#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__Õâžöº¯ÊýÓÃÀŽ·µ»ØÄ³žöÖµ¿ÉÒԺܺõÄÓÃÓÚ²éѯºÍ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属性表示返回到页面地址后边的调用函数,就是页面跳转的意思。

django+mysql+html简单demo之 views+html的更多相关文章

  1. Django + mysql 快速搭建简单web投票系统

    了解学习pyhton web的简单demo 1. 安装Django, 安装pyhton 自行百度 2. 执行命令创建project  django-admin.py startproject mysi ...

  2. 使用Django+MySQL+Apache+Linux创建简单的博客

    本教程基于慕课网<Django入门与实践>编写,基于CentOS 7 基础知识 什么是django? Django是一个基于Python的高级Web开发框架, 特点:高效,快速,高度集成( ...

  3. Django实战(一)之简单Demo

    菜鸟教程上Django安装可供参考: 参考链接: http://www.runoob.com/django/django-install.html 菜鸟教程上如果不行的话,下面博客网址可以供参考 Li ...

  4. Django入门3 简单留言板项目案例及mysql驱动的安装配置

    新建jangostart项目 使用manager.py新建app即单独的应用 创建一个message应用 manage.py@djangostart > startapp message 如果a ...

  5. Django:快速搭建简单的Blog

    一,创建项目 1, 为blog创建名为mysite的工程项目: django-admin.py startproject mysite 2, 项目结构如下: mysite ├── manage.py ...

  6. 用django创建一个简单的sns

    用django创建一个简单的sns 1.首先创建一个工程newsns django-admin.py startproject newsns 在工程目录下新建一个文件夹templates,在该文件夹下 ...

  7. django初探-创建简单的博客系统

    django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...

  8. django创建一个简单的web站点

    一.新建project 使用Pycharm,File->New Project…,选择Django,给project命名 (project不能用test命名)   新建的project目录如下: ...

  9. django初探-创建简单的博客系统(一)

    django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...

随机推荐

  1. 1、Docker介绍

      Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.容器完全使用沙箱机制,相互之间不会有任何接口 ...

  2. 2.自己的Github试用过程

    打开我个人的Github,我试着做些简单的试用.首先,经过简短描述,我成功创建了一个新的存储库

  3. django 定制管理页面外观 模板文件不生效的解决方法

    问题描述:大概过程跟下面描述的一样,简单来说就是照着例子学习的时候定制管理页面外观,按照文档要求拷贝了base_site.html文件到templates目录下,并且按照要求修改了settings.p ...

  4. api接口响应类型定义

    public class Response<T> { public ResponseStatus Status { get; set; } public string Message { ...

  5. VS2015 IIS Express 无法启动 解决办法(转)

    因为安装各种乱七八糟的软件,然后不小心把IIS Express卸载掉了,网上下载了一个IIS Express 7,安装之后本地使用VS 2015无法启动调试,F5 无法启动IIS, 再次F5调试,没有 ...

  6. 【转】如何成为一名优秀的web前端工程师(前端攻城师)?

    [转自]http://julying.com/blog/how-to-become-a-good-web-front-end-engineer/ 程序设计之道无远弗届,御晨风而返.———— 杰佛瑞 · ...

  7. 构造函数(JAVA)

    构造函数 :是一种特殊的方法,主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中. 特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类 ...

  8. Delphi中MessageBox用法

    消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.最简单用法,不带图形 MessageBox(0,'不同意','提示',MB_OK); MessageBo ...

  9. BZOJ4283: 魔法少女伊莉雅(最短路径图+最短路径树)

    题面 传送门 题解 太长了不想写了→_→ 题解 //minamoto #include<bits/stdc++.h> #define R register #define inf 0x3f ...

  10. ps与grep组合命令使用

    管道命令 我们在做运维的时候,经常会使用这个命令ps -ef | grep nginx. ps -ef 表示显示所有进程的消息. | 是管道命令.通常需要借助管道命令”|”多个命令的组合,形式如下: ...