Django表单get请求

创建一个 search.py 文件

-- coding: utf-8 --

from django.http import HttpResponse
from django.shortcuts import render_to_response
#使用 render_to_response可能会出错,可使用render
#from django.shortcuts import render

表单

def search_form(request):
return render_to_response(‘search_form.html’)

return render(request,‘search_form.html’,)

接收请求数据

def search(request):
request.encoding=‘utf-8’
if ‘q’ in request.GET and request.GET[‘q’]:
message = '你搜索的内容为: ’ + request.GET[‘q’]
else:
message = ‘你提交了空表单’
return HttpResponse(message)

urls.py 规则修改为如下形式:
url(r’^search-form ′ , s e a r c h . s e a r c h f o r m ) , u r l ( r ′ s e a r c h ', search.search_form), url(r'^search ,search.searchform),url(rsearch’, search.search),

访问地址 http://127.0.0.1:8000/search-form 并搜索

你可能感兴趣的:(Django)