我们在之前的项目中创建一个 search.py 文件,download网易云课堂:Vue+Django独立开发电商项目用于接收用户的请求:/HelloWorld/HelloWorld/search.py 文件代码:from django.http import HttpResponsefrom 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)在模板目录 templates 中添加 search_form.html 表单:/HelloWorld/templates/search_form.html 文件代码:菜鸟教程(runoob.com)
urls.py 规则修改为如下形式:/HelloWorld/HelloWorld/urls.py 文件代码:from django.conf.urls import urlfrom . import views,testdb,search urlpatterns = [ url(r'^hello/', testdb.testdb), url(r'^search-form/', search.search),]