用Django对用户提交的表单数据进行处理(get方式)。
search.py 文件代码:
from django.http import HttpResponse
from django.shortcuts import render
表单
def search_form(request):
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)
search_form.html 文件代码:
<form action="/search/" method="get">
<input type="text" name="q">
<input type="submit" value="搜索"> </form>
urls.py 文件代码:
from django.conf.urls import url
from . import views,testdb,search
urlpatterns = [
url(r'^hello/$', views.runoob),
url(r'^testdb/$', testdb.testdb),
url(r'^search-form/$', search.search_form),
url(r'^search/$', search.search),
]