在用户登录期间,使用正确的用户名和密码进行身份验证不会返回 None

2023-12-12

我的用户登录在身份验证过程中存在一些问题。 我正在使用 Django 1.9 和 Python 3.6

这是我的代码存储库

用户=验证(用户名=用户名,密码=密码)

将用户返回为无

这就是我的帐户/视图.py寻找登录

def register(request):
registered = False
if request.method == 'POST':
    reg_form = RegistrationForm(data=request.POST)
    profile_form = UserProfileForm(data=request.POST)
    if reg_form.is_valid() and profile_form.is_valid():
        user = reg_form.save()
        # print('before set password = ', user.password)
        user.set_password(user.password)
        # print('after set password = ', user.password)
        user.save()
        print(user.password)
        profile = profile_form.save(commit=False)
        profile.user = user
        profile.email = user.email
        profile.first_name = user.first_name
        profile.last_name = user.last_name
        if 'profile_pic' in request.FILES:
            profile.profile_pic = request.FILES['profile_pic']
            print('uploading pic .....')
        profile.save()
        args = {'reg_form': reg_form, 'profile_form': profile_form, 'registered': True}
        head_list.update(args)
        return render(request, 'registration.html', head_list)

    else:
        print(reg_form.errors, profile_form.errors)
        args = {'reg_form': reg_form.errors, 'profile_form': profile_form.errors, 'registered': False}
        head_list.update(args)
        return render(request, 'registration.html', head_list, args)
else:
    reg_form = RegistrationForm()
    profile_form = UserProfileForm()
    args = {'reg_form': reg_form, 'profile_form': profile_form, 'registered': False}
    head_list.update(args)
    print(head_list)
    return render(request, 'registration.html', head_list)


def login_view(request):
params = {}
params.update(csrf(request))
if request.method == 'POST':
    form = LoginForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        # First get the username and password supplied
        # username = request.POST.get('username', '')
        # password = request.POST.get('password', '')
        # Django's built-in authentication function:
        print(username, password)
        user = authenticate(username=username, password=password)
        print('after aunthenticate', user)
    # If we have a user
        if user:
            # Check it the account is active
            if user.is_active:
                # Log the user in.
                login(request, username)
                # Send the user back to some page.
                # In this case their homepage.
                # return HttpResponseRedirect(reverse('/user_login/'))
                return render_to_response('user_login.html', RequestContext(request, {}))
            else:
                # If account is not active:
                return HttpResponse("Your account is not active.")
        else:
            print("Someone tried to login and failed.")
            print("They used username: {} and password: {}".format(username, password))
            return HttpResponse("Invalid login details supplied.")

else:
    form = LoginForm()
    args = {'form': form}
    head_list.update(args)
    # Nothing has been provided for username or password.
    return render(request, 'login.html', head_list)

login.html页面如下所示

{% block content %}
    <section class="container">
    <h1>LiquorApp Login Console</h1>
        <div class="login">
            <h1>Login to WebApp</h1>
            <form method="post" action="/user_login/">
                {% csrf_token %}
                {{ form.as_p }}
                {% comment %}Username: <input type="text" name="username" value="" size="50" />
                <br />{% endcomment %}
                {% comment %}<p><input type="text" name="username" value="" id="username" placeholder="username"></p>
                <p><input id ="password" type="password" name="password" value="" placeholder="password"></p>
                <p class="remember_me">{% endcomment %}
                  <label>
                    <input type="checkbox" name="remember_me" id="remember_me">
                    Remember me on this computer
                  </label>
                </p>
                <p class="submit"><input type="submit" name="commit" value="Login"></p>
            </form>
        </div>
    </section>
{% endblock %}

请建议我在哪里做错了,我的身份验证模块没有返回任何内容。

我还在 settings.py 文件中添加了以下内容

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
)

Django 2.1 身份验证返回users对于任何身份验证,仅当user.is_active=TRUE你需要先保存响应保存(提交=False)然后设置自定义变量

if form.is_valid():
    user= form.save(commit=False)
    user.active=True
    user.staff=False
    user.admin=False
    user.save()
    messages.success(request, 'Account created successfully')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在用户登录期间,使用正确的用户名和密码进行身份验证不会返回 None 的相关文章

随机推荐

  • 从conftest.py访问测试文件名

    我正在尝试做什么 我正在使用 pytest 在 python 中编写一个小框架 作为拆卸的一部分 我正在截取屏幕截图 现在 我希望根据正在运行的测试来命名该屏幕截图 而不是 conftest py 例如 我现在的代码是 driver sav
  • 如何根据另一个字段中的值设置 SharePoint 列表字段中的默认值?

    我在 SharePoint 中有一个自定义列表 特别是 MOSS 2007 其中一个字段是标题为 有缺陷吗 的是 否复选框 另一个字段是 关闭者 指定关闭票证的人 如果没有缺陷 那么我希望票证自动关闭 如果有 则应稍后填写 关闭者 字段 我
  • Typescript :: 相关通用约束

    In React 我有一个像这样的通用界面 interface BaseProps
  • 解析 HTML 文本时的正则表达式与 XPath

    我想解析 HTML 文本并找到特殊部分 例如第 3 行中的文本div of 1st row和第二个column of a table 我有 2 个解析选项 正则表达式和 XPath 每一种的优点和缺点是什么 thanks 这在某种程度上取决
  • 获取运行脚本的父目录

    在 PHP 中 最简洁的方法是什么 parent当前运行脚本相对于 www 根目录的目录 假设我有 SERVER SCRIPT NAME relative path to script index php Or just something
  • 绝对位置是否使该元素成为包含块?

    在css2 1规范中 w3 org 有一个例子解释了包含块是如何形成的 p This is text em in the strong second strong paragraph em p 当位置em作为静态 strong的包含块是通过
  • 窗口大小更改时调整标题大小

    我目前正在开发一个网站 我需要在其中放置一些图像以及一些标题和描述 如果浏览器是全屏 则标题显示在一行上 但是 如果浏览器宽度减小 标题会自动显示为两行 这是浏览器全屏时的样子 这是当浏览器宽度减小时的样子 我想让它在浏览器宽度减小时减小标
  • 如何用C++模拟鼠标点击? [关闭]

    Closed 这个问题需要细节或清晰度 目前不接受答案 我需要模拟单击应用程序窗口的鼠标单击 我正在使用 Windows 如何将鼠标左键单击发送到屏幕x y窗口所在的坐标 Use the 发送输入 功能 INPUT Inputs 3 0 I
  • 带 IdHTTP 的 POST 请求

    您好 我正在尝试使用组件 IdHTTP POST 方法填写表单 我的代码是这样的 var par2 TIdMultiPartFormDataStream rta string begin par2 TIdMultiPartFormDataS
  • GAE:此应用程序不允许 API 服务

    我正在尝试按照以下教程进行操作 http www youtube com watch v v9TG7OzsZqQ 我的 Cloud Endpoint REST API 在本地开发计算机上运行良好 但当我部署到 App Engine 时 我的
  • 调用另一个合约中的函数 - Solidity

    我需要使用 Truffle 调用另一个合约中的函数 这是我的合同样本 类别 sol contract Category notice Check if category exists function isCategoryExists ui
  • C# 按列按字母顺序对列表进行排序

    我定义了一个类 并将该类的记录写入列表中 在编写错误报告之前无法对列表进行排序 我试图在写入错误报告之前按 finderror 类型按字母顺序对列表进行排序 以便在错误报告中对列表进行排序并更有条理 这是课程 public class ty
  • 如何要求套接字等待更多数据到来

    我正在玩保留CLI项目 它是一个与统计环境 R 通信的 net 客户端 基本思想是通过 TCP 协议在此 NET 客户端和 R 会话之间发送数据 命令 我和其他人发现的一个错误是大数据主干 比如超过 10k 字节 无法成功传输 我在下面的代
  • C++11 中的可变长度结构非标准? [复制]

    这个问题在这里已经有答案了 可能的重复 struct hack 在技术上是未定义的行为吗 我检查了 C 11 中是否允许零长度数组 看来他们不是 从8 3 4 Arrays dcl array 如果存在常量表达式 5 19 则它应是整型常量
  • Android 5 ADB通知转储错误数据

    我构建了一个电脑程序 可以在您的桌面上运行 通知您有关通知 电量级别的信息 并让您与智能手机进行交互 整个 ADB 因此不需要手机上的应用程序 所有这一切都以一种时尚的方式进行 但这已经足够了 直到 Android 5 出现为止 通知转储
  • Bash 正则表达式在句子中查找特定单词

    我有一句话是这样的 The dog jumped over the moon because he likes jumping 我想找到所有匹配的单词jump i e jumped and jumping 我怎样才能做到这一点 目前我有一个
  • 从 Internet 读取 Inno Setup 加密密钥而不是密码框

    我希望设置从 HTTP 读取密码GET请求而不是直接来自用户 有什么方法可以绕过密码框并执行此操作吗 使用读取密钥WinHttpRequest对象 将其插入密码框并提交密码页面 Setup Password 123 Encryption y
  • 获取列值不为 NULL 的每个列计数

    我有一个表 表中有 5 列 我想要列值不为空的每一列的行数 column1 column2 column3 column4 column5 1 2 2 2 2 2 2 2 NULL 2 3 NULL 2 2 NULL NULL NULL 2
  • 如何使用 Capybara + Poltergeist 将文本设置到 Summernote 文本区域

    我有一个正在使用的文本区域夏日笔记我给它设置了 onChange 事件 我想用 RSpec Capybara Poltergeist 编写测试来确认 onChange 事件正常工作 据我检查 浏览器中显示的文本区域实际上是带有 note e
  • 在用户登录期间,使用正确的用户名和密码进行身份验证不会返回 None

    我的用户登录在身份验证过程中存在一些问题 我正在使用 Django 1 9 和 Python 3 6 这是我的代码存储库 用户 验证 用户名 用户名 密码 密码 将用户返回为无 这就是我的帐户 视图 py寻找登录 def register