我的代码显示错误:
method not allowed: POST
method not allowed: /home/
在运行服务器时,我找不到错误在哪里,每当我在我的 html 表单中单击搜索时,它再次将我重定向到http://127.0.0.1:8000/home/ 这是我的应用程序的 view.py
class HomePageView(TemplateView):
template_name = 'home.html'
class ResultPageView(TemplateView):
template_name = 'results.html'
def search(request):
if request.method == 'POST':
MYSearch = Searchform(request.POST)
if form.is_valid():
return redirect('/thanks/')
else:
MYSearch = Searchform()
return render(request, 'results.html',{"MYSearch":MYSearch})
表格.py
class Searchform(forms.Form):
source = forms.CharField(max_length=100)
destination = forms.CharField(max_length=100)
网址.py
urlpatterns = [
path('results/', ResultPageView.as_view(), name='results'),
path('home/', HomePageView.as_view(), name='search'),
]
编辑 1
我也为表单添加了我的 html 代码
<form name = "form" action = "{% url "search" %}"
method = "POST" >{% csrf_token %}
<div style = "max-width:470px;">
<center>
<input type = "text"
placeholder = "source" name = "source" />
</center>
</div>
<div style = "max-width:470px;">
<center>
<input type = "text"
placeholder = "destination" name = "destination" />
</center>
</div>
<button type = "submit" value = "Search" >
<strong>Search</strong>
</button>