django+mysql+html简单demo之 views+html
#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的更多相关文章
- Django + mysql 快速搭建简单web投票系统
了解学习pyhton web的简单demo 1. 安装Django, 安装pyhton 自行百度 2. 执行命令创建project django-admin.py startproject mysi ...
- 使用Django+MySQL+Apache+Linux创建简单的博客
本教程基于慕课网<Django入门与实践>编写,基于CentOS 7 基础知识 什么是django? Django是一个基于Python的高级Web开发框架, 特点:高效,快速,高度集成( ...
- Django实战(一)之简单Demo
菜鸟教程上Django安装可供参考: 参考链接: http://www.runoob.com/django/django-install.html 菜鸟教程上如果不行的话,下面博客网址可以供参考 Li ...
- Django入门3 简单留言板项目案例及mysql驱动的安装配置
新建jangostart项目 使用manager.py新建app即单独的应用 创建一个message应用 manage.py@djangostart > startapp message 如果a ...
- Django:快速搭建简单的Blog
一,创建项目 1, 为blog创建名为mysite的工程项目: django-admin.py startproject mysite 2, 项目结构如下: mysite ├── manage.py ...
- 用django创建一个简单的sns
用django创建一个简单的sns 1.首先创建一个工程newsns django-admin.py startproject newsns 在工程目录下新建一个文件夹templates,在该文件夹下 ...
- django初探-创建简单的博客系统
django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...
- django创建一个简单的web站点
一.新建project 使用Pycharm,File->New Project…,选择Django,给project命名 (project不能用test命名) 新建的project目录如下: ...
- django初探-创建简单的博客系统(一)
django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...
随机推荐
- java并发编程实战:第十四章----构建自定义的同步工具
一.状态依赖性管理 对于单线程程序,某个条件为假,那么这个条件将永远无法成真 在并发程序中,基于状态的条件可能会由于其他线程的操作而改变 可阻塞的状态依赖操作的结构 acquire lock on o ...
- OpenSSL命令---crl2pkcs7
用途: 本命令根据CRL或证书来生成pkcs#7消息. 用法: openssl crl2pkcs7 [-inform PEM|DER ] [-outform PEM|DER ] [-in filena ...
- pro2
#include<iostream> double sum(int n,dounle[]) { double array[100]; foe(int i=0;i<100;i++; ...
- datetime.date(2014, 4, 25) is not JSON serializable
# 背景 接口期望返回json格式数据,但数据存储在mysql中,先将mysql的数据转为dict,然后将dict转为json格式,然后就报这个错误了的,原因就是时间格式转换问题 # 解决方法 1. ...
- 用.msi安装node时安装失败,出现rolling back action(转载)
转载地址:https://blog.csdn.net/qq_33295622/article/details/52956369在重装node时出现了上图所示情况,解决方法如下: 1.在官网下载稳定版本 ...
- html开发基础
1 Doctype Doctype告诉浏览器使用什么样的html或xhtml规范来解析html文档 有和无的区别 BackCompat:标准兼容模式未开启(或叫怪异模式[Quirks mode].混杂 ...
- web思维导图(前期)
- Redis安装与测试
①安装,直接安装版本为4.0.1 sudo apt-get install redis-server 启动: redis-server redis-cli 测试: ②新建Student表 ③查看zha ...
- memcache面试题汇总
1,memcached是原子的吗? 所有的被发送到memcached的单个命令是完全原子的.如果您针对同一份数据同时发送了一个set命令和一个get命令,它们不会影响对方.它们将被串行化.先后执行.即 ...
- php全局变量漏洞 $GLOBALS
在Discuz代码中有这么一段: if (isset($_REQUEST[‘GLOBALS’]) OR isset($_FILES[‘GLOBALS’])) { exit(‘Request tain ...