0

我的代码显示错误:

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>
4

1 回答 1

0

由于您使用的是TemplateView,因此两个页面都实现了自动 GET 方法。但搜索功能实际上是在尝试捕获home.html的 POST 请求。阅读此内容,了解何时以及何时不使用TemplateView

https://www.agiliq.com/blog/2017/12/when-and-how-use-django-templateview/

如果我是正确的,搜索功能就是您呈现表单的地方,它只允许 GET 请求。所以应该是

def search(request):
    my_search = SearchForm()
    return render(request, 'home.html',{"MYSearch": my_search}) # Render the form in home page

def results(request):
    if request.method == 'POST':
        my_search = SearchForm(request.POST)
        if form.is_valid():
            return render(request, 'results.html')

因此,您可以摆脱HomePageViewResultPageView类并将 URL 设置为,

path('results/', views.results, name='results')
path('home/', views.search, name='search')

本质上,有一种更短的方法可以做到这一点,将这两个功能组合成一个,即,如果是 GET 请求,则呈现表单,如果是 POST 请求,则查看结果。

def search(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = SearchForm(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 render(request, 'result.html', # parameters to pass, if any)

    # if a GET (or any other method) we'll create a blank form
    else:
        form = NameForm()

    return render(request, 'name.html', {'form': form})

那么 URL 将是,

path('results/', views.search(), name='results')
path('home/', views.search(), name='search')

它在官方文档中有描述。

https://docs.djangoproject.com/en/3.0/topics/forms/

您还需要在 html 文件中更改 form 的 action 属性。

<form action="result/">

有关完整示例,请查看: https ://djangobook.com/mdj2-django-forms/

于 2020-06-02T19:08:36.497 回答