Django 表单问题:“WSGIRequest”对象没有属性“get”

2024-02-15

我在 Django 调试视图中收到此错误:

'WSGIRequest' object has no attribute 'get'

这是用于登录脚本的,其中大部分是从管理代码复制的,主要用于练习/调整原因。我在views.py中的代码如下:

@sensitive_post_parameters()
@csrf_protect
@never_cache
def login(request, template_name="main/login.html",
          authentication_form=LoginForm,
          redirect_field_name=REDIRECT_FIELD_NAME,
          current_app=None, extra_context=None):
    """
    Displays the login form and handles the login action.
    """
    redirect_to = request.REQUEST.get(redirect_field_name, '')

    if request.method == "POST":
        form = authentication_form(request, data=request.POST)
        if form.is_valid():

            # Ensure the user-originating redirection url is safe.
            if not is_safe_url(url=redirect_to, host=request.get_host()):
                redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

            # Okay, security check complete. Log the user in.
            user = get_user(request.POST.get('email'))
            auth_login(request, user)

            return HttpResponseRedirect(redirect_to)
    else:
        form = authentication_form(request)
    current_site = get_current_site(request)

    context = {
        'form': form,
        redirect_field_name: redirect_to,
        'site': current_site,
        'site_name': current_site.name,
    }
    if extra_context is not None:
        context.update(extra_context)
    return TemplateResponse(request, template_name, context,
                            current_app=current_app)

这是我的表格:

class LoginForm(forms.Form):
    email = forms.CharField(max_length=100, label="email", widget=forms.TextInput(attrs={'class': 'form-control'}), required=True)
    password = forms.CharField(max_length=32, label="password", widget=forms.PasswordInput(attrs={'class': 'form-control'}), required=True)

我在 login.html 第 13 行收到错误

{% extends 'base_main.html' %}
{% load staticfiles %}

{% block title %}Login{% endblock %}
{% block stylesheet %}
    {{ block.super }}
    <link rel="stylesheet" type="text/css" href="{% static 'main/css/login.css' %}">
{% endblock %}

{% block body %}
    <div class="container">
        {% if form %}
            <!--
            <form class="form-signin" role="form" method="POST" action="/check_login/">{% csrf_token %}-->
                <h2 class="form-signin-heading">Please sign in</h2>
                {{ form }} <!-- Line 13 -->
                <!--<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
            </form>
            -->
        {% endif %}
    </div>
{% endblock %}

有人对我如何解决这个问题有任何想法吗?

Update

Traceback:
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  139.                 response = response.render()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  305.                 return nodelist.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render
  91.             output = force_text(output)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
  100.                 s = s.__unicode__()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in __str__
  103.         return self.as_table()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in as_table
  223.             errors_on_separate_row = False)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in _html_output
  148.         top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in non_field_errors
  249.         return self.errors.get(NON_FIELD_ERRORS, self.error_class())
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in errors
  121.             self.full_clean()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
  273.         self._clean_fields()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in _clean_fields
  282.             value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/widgets.py" in value_from_datadict
  207.         return data.get(name, None)

Exception Type: AttributeError at /login/
Exception Value: 'WSGIRequest' object has no attribute 'get'

问题出在这几行:

form = authentication_form(request, data=request.POST)
...
form = authentication_form(request)

Your LoginFormclass' init 方法不会像下面那样接受请求对象AuthenticationForm from django.contrib.auth.forms does.

所以你可以这样做:

form = authentication_form(data=request.POST)
...
form = authentication_form()

...但实际上可能是您当前的LoginForm类做得不够,你应该看看 Django 类(特别是检查会话 cookie 等的东西),可能是它的子类。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django 表单问题:“WSGIRequest”对象没有属性“get” 的相关文章

随机推荐

  • 企业报告解决方案[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • ListView Flutter 的单选

    我正在尝试实施一个listView在我的应用程序中进行单选 这样一旦点击列表中的项目 按下的项目颜色状态就会与其他项目不同 我已经做了我所知道的一切 但效果并不好 问题是 即使我的实现在按下时更新每个项目的状态 它也不会将其他项目重置为其初
  • 无法将 Web.Http.Results.JsonResult 隐式转换为 Web.Mvc.JsonResult

    我已经在控制器上设置了这个测试方法 以消除任何复杂性 根据我通过搜索找到的所有结果 这应该可行 我不确定我在这里缺少什么 public JsonResult test return Json new id 1 这是我得到的错误 无法将类型
  • 在 PHP 5 中如何通过引用传递对象?

    在 PHP 5 中 您是否需要使用 修饰符通过引用传递 例如 class People p new People function one a a null function two a a null 在 PHP4 中你需要 修改器来在更改
  • 缓冲读取器和文件读取器以及扫描器类之间的区别[重复]

    这个问题在这里已经有答案了 谁能解释一下班级之间的区别BufferedReader FileReader and Scanner 当我想读取文本文件时该使用哪一个 Well FileReader只是一个Reader它使用平台默认编码 urg
  • KJUR jws jsrsasign:无法在 JWT.io 上验证 ES256 令牌

    我们正在尝试使用 KJUR jws 库为 Apple Search Ads 制作 JWT 令牌 我们使用的是Apple的API文档 https developer apple com documentation apple search a
  • GridView PageIndexChanging 不起作用

    这似乎是一个很容易问的问题 但我无法在 GridView 中显示项目 这是我的代码 public partial class TestList System Web UI Page protected void Page Load obje
  • 在圆上找到最接近给定点的点的最佳方法

    给定一个点 pX pY 和一个已知圆心 cX cY 和半径 r 的圆 您可以想出最短的代码量来找到圆上最接近 pX pY 我有一些代码可以工作 但它涉及将圆转换为 x cX 2 y cY 2 r 2 其中 r 是半径 形式的方程并使用方程绘
  • 了解代码优先的虚拟属性

    您好 我刚刚学习使用实体框架代码优先 我似乎无法理解一些东西 我根据教程创建了三个模型 public class Course public int CourseID get set public string Title get set
  • python脚本在windows中运行时是什么用户? [复制]

    这个问题在这里已经有答案了 我试图让 python 删除一些目录 但出现访问错误 我认为是python用户帐户没有权限 WindowsError Error 5 Access is denied path 是我运行脚本时得到的结果 我试过了
  • 映射结构:使用“.”作为目标属性 - 目前是否支持?

    我想按如下方式使用映射结构 Mapping target source object1 subobject2 subobject3 public abstract Object4 toObject Object1 object1 作为对象
  • KML 图层限制问题

    我已通过 url 将 6 kml 图层加载到我的网站 以便通过复选框关闭 打开 但我最近注意到 它只允许我在给定时间显示 4 公里 当我选择超过 4 个时 第 5 个和第 6 个不会显示 我选择什么并不重要 它似乎限制我只能显示 4 有人可
  • Asp.net core中ViewComponent之间如何共享ViewData

    我有两个 ViewComponent 我想使用 ViewData 或其他技术来共享一些数据 然后在主视图中使用这些数据 但这不是方法 当两个 ViewComponent 的 if 条件丰富时 每个 ViewComponent 的 ViewD
  • .ics 邀请日历在 Outlook.com 问题中不起作用

    您好 我使用 cakephp 电子邮件发送一封附有 ics 日历的电子邮件 问题是确认按钮 si 完美显示在 yahoo 和 gmail 中 但不在 Outlook com 中 以下是 ics 文件的示例 BEGIN VCALENDAR V
  • DataGridView 单元格编辑十进制/十六进制格式的问题

    我有一个数据网格视图绑定到一个数据表1 16 列定义为Integer 默认单元格样式为十六进制 2 位数字 Format X2 当进入单元格编辑时 我想向用户提供以十进制或十六进制写入值的可能性 十六进制可以写成 例如 0x00 0X01
  • 错误消息显示退出代码-1073741510 [重复]

    这个问题在这里已经有答案了 我正在尝试使用以下 cod 读取 cpuid 信息 但它不起作用 我正在使用 Visual Studio 2010 include stdafx h include
  • 如何从文件列表开始创建电影数据库

    我的家庭服务器上有大量电影 大约 4000 部 文件全部命名为Title Subtitle year extension 我想创建一个包含我所有电影的数据库 即使在 Excel 中也可以 数据库应包含以下列 标题 副标题 如果存在 年份和文
  • 获取redis中存在的所有哈希值

    我在 redis 缓存中有哈希值 如下所示 Hash Key Value hashme 1 Hello World hashme 2 Here Iam myhash 1 Next One 我的目标是在 CLI 中获取哈希值作为输出 如下所示
  • 无法解析符号“LocationServices”

    我正在尝试使用使用用户位置的 android studio 构建一个 android 应用程序 我正在尝试导入 google play services LocationServices api 但它说它无法解析符号 LocationSer
  • Django 表单问题:“WSGIRequest”对象没有属性“get”

    我在 Django 调试视图中收到此错误 WSGIRequest object has no attribute get 这是用于登录脚本的 其中大部分是从管理代码复制的 主要用于练习 调整原因 我在views py中的代码如下 sensi