OR',
#             'propagate': True,
#             },
#         }
# }

Django框架中的基本交互

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 服务器端展示数据
from django.shortcuts  import  render_to_response
def search(request):
     return  render_to_response( 'search.html' , { 'books' :books,})
#
# search.html的数据渲染 , 利用模板
{%  if  books %}
     <ul>
     发现 {{ books | length }} 本书
     {%  for  book  in  books %}
         <li>{{ book.title }}< /li >
     {% endfor %}
     < /ul >
{%  else  %}
     <p>没有查询到任何图书信息< /p >
{% endif %}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 客户端提交数据 search_form.html
<html><head>
<title>Search< / title>< / head>
<body>
{ %  if  error  % }
<p style = "color:red;" >请输入查询条件< / p>
{ %  endif  % }
<form action = "/search_form/"  method = "get" >    
< input  type = "text"  name = "q" >    
< input  type = "submit"  value = "Search" >
< / form>
< / body>
< / html>
#
# 收集客户端信息
# from django.http import HttpResponse
# request.path()   get_host()   get_full_path()   get_isecure()
# request.META[]   包含http头信息
def  search_form(request):
     if  'q'  in  request.GET  and  request.GET[ 'q' ]:
         =  request.GET[ 'q' ]
         books  =  Book.objects. filter (title__icontains = q)
         return  render_to_response( 'search_result.html' , { 'books' :books, 'query' :q})
     else :
         return  render_to_response( 'search.html' , { 'error' True })


最后附博文中基本实现代码一份


下一篇计划Django的高级应用