一、创建HTML的FORM表单网上教程大多是旧版django,本教程记录我解决form表单的一个问题
index.html
二、创建对应的视图views.py
def search(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
message = request.POST.get('word')
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponse(f'')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'index.html', {'form': form})
三、构建一张Form类表单
forms.py
from django import forms
class NameForm(forms.Form):
word = forms.CharField(label='搜索', max_length=100)
四、urls调度器
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('search/', views.search,name = 'search')
]



