django+mysql+html简单demo之 views+html

 1  #coding=utf-8
 2 from __future__ import unicode_literals
 3 
 4 from django.shortcuts import render,render_to_response
 5 from django.http import HttpResponse
 6 
 7 from django.template import loader
 8 from books import models
 9 
10 # Create your views here.
11 
12 def book_page(request):
13     #取出指定字段所有值
14     #name_list=models.Publisher.objects.values('name',"city")
15     #get查找,只能查询一条数据
16     #name_list=models.Publisher.objects.get(id=1)
17     
18     #修改数据,改完再获取
19     #models.Publisher.objects.filter(id=1).update(city="chengdu")
20     #name_list=models.Publisher.objects.get(id=1)
21 
22     #修改数据,获取后再改。
23     #name_list=models.Publisher.objects.get(id=1)
24     #name_list.city="guangzhou"
25     #name_list.save()
26 
27     #删除数据,找不到,会抛出 Publisher matching query does not exist。
28     models.Publisher.objects.filter(id=2).delete()
29     name_list=models.Publisher.objects.get(id=2)
30 
31     #name_list=models.Publisher.objects.all()
32     #name_list=[{'name':'zhangsan','city':'beijing'},{'name':'lisi','city':'shanghai'}]
33     return render_to_response('show.html',{'name_list':name_list})
34 
35 
36 def search_form(request):
37     return render_to_response('form.html',{})
38 
39 def search(request):
40     if 'name' in request.GET and request.GET['name']:
41         name=request.GET['name']
42         books=models.Book.objects.filter(title__icontains=name)
43         return render_to_response('search.html',{'books':books,'query':name})
44     else:
45         return render_to_response('form.html',{'error':True})
46 
47 def search_form1(request):
48     return render_to_response('form0.html',{})
49 
50 def search1(request):
51     errors=[]
52     if 'name' in request.GET:
53         name=request.GET['name']
54         if not name:
55             errors.append("Enter a content.")
56         elif len(name)>20:
57             errors.append("Please enter less than 20 code.")
58         else:
59             books=models.Book.objects.filter(title__icontains=name)
60             return render_to_response('search.html',{'books':books,'query':name})
61     
62     return render_to_response('form0.html',{'errors':errors})
views.py
 1 # -*- coding: utf-8 -*-
 2 from __future__ import unicode_literals
 3 
 4 from django.db import models
 5 
 6 # Create your models here.
 7 class Publisher(models.Model):
 8     name = models.CharField(max_length = 30)
 9     address = models.CharField(max_length=50)
10     city = models.CharField(max_length=60)
11     state_province = models.CharField(max_length=30)
12     country = models.CharField(max_length=50)
13     website = models.URLField()
14 #__unicode__Õâžöº¯ÊýÓÃÀŽ·µ»ØijžöÖµ¿ÉÒԺܺõÄÓÃÓÚ²éѯºÍadminœçÃæµÄÏÔÊŸ
15     def __unicode__(self):
16         return self.name
17 
18 class Author(models.Model):
19     first_name = models.CharField(max_length=30)
20     last_name = models.CharField(max_length=40)
21     emial = models.EmailField(blank=True,verbose_name = 'e-mail')
22     def __unicode__(self):
23         return u'%s %s'%(self.first_name,self.last_name)
24 
25 class Book(models.Model):
26     title = models.CharField(max_length = 100)
27     author = models.ManyToManyField(Author)
28     publisher = models.ForeignKey(Publisher)
29     publication_date = models.DateField(blank = True,null = True)
30     def __unicode__(self):
31         return self.title
models.py
 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="utf-8">
 5     <title>查询</title>
 6   </head>
 7   <body>
 8     {% if error%}
 9     <ul>
10       {% for error in errors%}
11       <li> {{error}}</li>
12       {%endfor%}
13     </ul>
14     {%endif%}
15     <form class="" action="/search/" method="get">
16         <input type="text" name="name" value="">
17         <input type="submit" name="submit" value="提交">
18     </form>
19   </body>
20 </html>
form.html
 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="utf-8">
 5     <title>查询</title>
 6   </head>
 7   <body>
 8     {% if errors%}
 9     <ul>
10       {% for error in errors%}
11       <li> {{error}}</li>
12       {%endfor%}
13     </ul>
14     {%endif%}
15     <form class="" action="/search1/" method="get">
16         <input type="text" name="name" value="">
17         <input type="submit" name="submit" value="提交">
18     </form>
19   </body>
20 </html>
form0.html
 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="utf-8">
 5     <title>result</title>
 6   </head>
 7   <body>
 8     <p> search for: <strong>{{query}}</strong>  </p>
 9     {% if books%}
10     <p>
11       found {{books | length }} book {{books | pluralize}}
12      </p>
13      <ul>
14        {% for book in books%}
15        <li>{{book.title}}</li>
16        {%endfor%}
17      </ul>
18      {%else%}
19      <p>
20        No books matched your search.
21      </p>
22      {% endif%}
23   </body>
24 </html>
search.html
 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta charset="utf-8">
 5     <title>show</title>
 6   </head>
 7   <body>
 8     <table border="1">
 9       <thead>
10         <tr>
11           <td> Name</td>  
12          <td>   City  </td>
13          <td> address  </td>
14           <td> state_province</td>
15           <td>   country  </td>
16           <td>    website  </td>
17         </tr>
18       </thead>
19       <tbody>
20         {#% for line in name_list%#}
21          <tr>
22            <td>   {{line.name}} </td>
23            <td>   {{line.city}} </td>
24             <td>   {{line.address}} </td>
25              <td>   {{line.state_province}} </td>
26               <td>   {{line.country}} </td>
27                <td>   {{line.website}} </td>
28          </tr>
29          {#% endfor%#}
30          <tr>
31            <td>   {{name_list.name}} </td>
32            <td>   {{name_list.city}} </td>
33             <td>   {{name_list.address}} </td>       
34       </tbody>
35     </table>
36   </body>
37 </html>
show.html

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

转载于:https://www.cnblogs.com/chengyunshen/p/7196240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值