【问题标题】:Method Not Allowed: / (Django-Post Method)不允许的方法:/(Django-Post 方法)
【发布时间】:2019-03-17 18:37:33
【问题描述】:

当我提交表单时,控制台中显示“方法不允许:/”..

类似的东西: 不允许的方法:/ [17/Mar/2019 18:31:18]“POST/HTTP/1.1”405


我在 views.py 文件中使用它。

class UrlAccpt(View):

    template_name='phc/url_accpt.html'

    def get(self,request):
        urlx=''
        form = UrlForm(request.POST)
        if request.method == 'POST':
            form = UrlForm(request.POST)
            if form.is_valid():
                urlx= form.cleaned_data['EnterTheUrl']

        form = UrlForm(request.POST)

    response = TemplateResponse(request,self.template_name,{'form':form,'value':urlx})
    return response

在 forms.py 文件中...我使用此代码

from django import forms


class UrlForm(forms.Form):

    EnterTheUrl=forms.CharField(max_length=1000)

【问题讨论】:

    标签: django django-forms django-views django-2.1


    【解决方案1】:

    欢迎使用基于类的视图:

    你需要在你的类中指定 post 函数。获取功能仅在 GET 方法上触发,不适用于 POST 请求。

    添加以下功能并将您的帖子逻辑移至此处...

    def post:
       ...
    

    看看docs

    【讨论】:

      【解决方案2】:

      基于类的视图不能以这种方式工作。您必须为要涵盖的每个 http 方法类型定义一个方法(至少如果您只是从 View 继承)类。因此,在基于类的视图中为这样的帖子定义一个方法,它将起作用

      class UrlAccpt(View):
      
          template_name='phc/url_accpt.html'
      
          def get(self,request):
              urlx=''
              form = UrlForm()
      
         def post(self,request, *args, **kwargs):
              form = UrlForm(request.POST)
              if form.is_valid():
                  urlx= form.cleaned_data['EnterTheUrl']
      

      你可以在thisdoc的Supporting other HTTP methods中了解它

      【讨论】:

      • 非常感谢您的帮助...这里是菜鸟
      猜你喜欢
      • 1970-01-01
      • 2017-07-16
      • 2021-04-05
      • 2018-05-30
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多