django - 不允许的方法 :/(Django-Post Method)

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

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

类似这样的事情: 不允许的方法:/ [2019年3月17日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)

最佳答案

欢迎来到基于类的 View :

您需要在类中指定 post 函数。 Get 函数仅在 GET 方法上触发,不适用于 POST 请求。

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

def post:
   ...

看看docs

关于django - 不允许的方法 :/(Django-Post Method),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55210567/

相关文章:

python - 名称 'Q' 未定义

Django 表单向导 - 每个步骤的自定义表单布局

python - Django 中的删除功能不起作用

python - 为什么 python __file__ 变量在 DJango 中使用时返回错误路径?

javascript - 未捕获的引用错误 : Unexpected Token }

django - Django i18n:如何不翻译管理网站?

python - django.db.utils.OperationalError : (2026, 'SSL connection error: SSL_CTX_set_tmp_dh failed' )

django - 限制用户使用特定域注册: django

django - 通过 django 跟踪打开的电子邮件

Django 重定向并带有自定义错误消息