【问题标题】:Django | 'function' object has no attribute 'get' | for request.POST.get('email')姜戈 | 'function' 对象没有属性 'get' |对于 request.POST.get('email')
【发布时间】:2019-10-16 06:57:01
【问题描述】:

我下载了一个 HTML 模板表单并在我的 HTML 页面中使用并从中获取数据。

view.py 中与之关联的函数正在运行。

request.POST.get('name') 正在返回输入的文本,但在重定向页面上出现错误。

views.py 中的函数

def login(request):
    if request.method == "POST":
        email = request.POST.get('email')
        password = request.POST.get('pass')
        print(email)
        print(password)
        return redirect
    return render(request, 'taskManagementWebApp_templates/login.html')

点击按钮时的终端输出

abc@gmail.com
123456789
Internal Server Error: /login/
Traceback (most recent call last):
  File "C:\Users\Akestech\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\Akestech\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\deprecation.py", line 96, in __call__
    response = self.process_response(request, response)
  File "C:\Users\Akestech\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'function' object has no attribute 'get'
[16/Oct/2019 12:16:55] "POST /login/ HTTP/1.1" 500 60650

网页浏览器出错 Screenshot of Error during the calling of redirect

【问题讨论】:

    标签: python django templates django-forms django-views


    【解决方案1】:

    返回重定向

    此行无效。

    redirect 应该是这样的:

    redirect(to, *args, permanent=False, **kwargs)
    

    to 可能在哪里:

    1. 模型:将调用模型的 get_absolute_url() 函数。
    2. 视图名称,可能带有参数:reverse() 将用于反向解析名称。
    3. 绝对或相对 URL,将按原样用于重定向位置。

    查看Django's official documentation 并根据您的需要进行更新。

    【讨论】:

      【解决方案2】:

      您返回的是redirect,即a function that creates a redirect response

      你必须调用它并返回结果,例如

      return redirect("/some-path/")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-13
        • 2021-06-12
        • 2016-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-21
        • 2019-08-13
        相关资源
        最近更新 更多