#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. 如何把asp.net上的服务在iis调试

    1,在iis上部署一个应用,路径指向工程目录 2,在vs2012(其他版本没试过,10都很久没用不记得了)中打开网站,选择本地iis 3,在你想调试的地方设置断点,F5,搞定!

  2. redis修改端口号

    为redis分配一个8888端口,操作步骤如下: 1.$REDIS_HOME/redis.conf重新复制一份,重命名为redis8888.conf. 2.打开redis8888.conf配置文件,找 ...

  3. 谈谈INotifyPropertyChanged和ICommand

    WPF,Windows8和Windows Phone开发中的MVVM设计模式中很重要的两个接口是INotifyPropertyChanged和ICommand,深入理解这两个接口的原理,并掌握其正确的 ...

  4. Solr之functionQuery(函数查询)

    Solr函数查询 让我们可以利用 numeric域的值 或者 与域相关的的某个特定的值的函数,来对文档进行评分. 怎样使用函数查询 这里主要有两种方法可以使用函数查询,这两种方法都是通过solr ht ...

  5. 解决AttributeError: 'Ui_MainWindow' object has no attribute 'show'报错

    1.首先使用pyqt designer来设计ui界面,将其保存为"***.ui"文件, 然后进入到pyqt所在的文件目录中,执行cmd中命令,即在当前目录中可以生成相应的**.py ...

  6. Jenkins权限管控

    需求: 不同的账号角色进入只能看到自己对应的项目,且只能拥有构建等基本权限. 如wechat用户进入系统只能看到以wechat开头的job(具体匹配什么名称的job,可以设置) 目录: 1.安装插件 ...

  7. 日笔记--C# 从数据库取表格到DataGridView---json传输

    只作为个人学习笔记. class OpData { // 创建一个和客户端通信的套接字 Socket socketwatch = null; //连接Access字符串 string strCon; ...

  8. pod-infrastructure:latest镜像下载失败

    报错一:image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be be ...

  9. linux 使用内存作为 /tmp 临时文件夹

    方法一:cat /etc/fstabtmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 方法二:mount tmpfs /tmp -t tmpfs -o s ...

  10. poj做的题

    1699 1080 1170 1239(不错) 1659(读读怎么写)