本实例目的是在前端网页中创建一个搜索框,把数据反馈到后台,后台处理后把数据返回到网页的过。
实例如下:
前端模板index.html代码
主要是一个表单,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action={% url 'book:book' %}>
<input type="text" name="book_deta">
<input type="submit" value="提交">
</form>
</body>
</html>
视图后端处理简单代码
from django.shortcuts import render,HttpResponse
def index(request):
return render(request,'index.html')
def book_search(request):
book_data=request.GET.get('book_deta')
#获取前端提交数据。
return HttpResponse("{}".format(book_data))
URLS.PY
from django.urls import path
from .views import index,book_search
urlpatterns = [
path('',index,name='index'),
path('books',book_search,name='book'),
# path('news/',news,name='news'),
]
效果如下图:
输入ABC提交效果