python - Django:创建新用户帐户

标签 python django

我在创建新帐户然后登录时遇到一些问题。我输入了所有凭据(名字、姓氏、用户名、密码),然后选择“创建新帐户”,它成功地将我重定向回登录页面。但是,当我尝试使用这个新帐户登录时,它说我的用户名不存在。

问题很可能出在我的views.py 文件中:

def create_account(request):
    if request.method == 'POST':
        new_user = User(username = request.POST["username"], 
                        password = request.POST["password"])
        new_user.save()
        Student.objects.create(user=new_user, 
                               first_name=str(request.POST.get("first_name")),
                               last_name=str(request.POST.get("last_name")))
        new_user.is_active = True
        return redirect('../')
    else:
        return render(request, 'polls/create_account.html')

如果你们需要更多代码或信息,请告诉我。谢谢!

最佳答案

密码字段需要加密。如果要设置密码,则需要使用 set_password() 方法来处理加密。

        new_user = User(username = request.POST["username"])
        new_user.set_password(request.POST["password"])
        new_user.save()

关于python - Django:创建新用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34908229/

相关文章:

python - 如何正确地将Python库导入到Django项目(托管在Heroku上)?

python - 超链接到 Sphinx PDF 文档中的外部 git 存储库

python - Django 迁移损坏,不会忘记已删除的列

python - 在 Django 中翻译文件时的 Git 命令

python - 类型 'HttpResponse' 的对象的 Django 分页错误没有 len()

ajax - 如何将Django错误状态传递给Ajax

python - 如何设置 Atom 的脚本来运行 Python 3.x 脚本?与 Windows 7 Pro x64 的组合可能是问题所在吗?

python - 将天数添加到当前日期python

python - 字符串的基本递归?

Django 表单 : how to dynamically create ModelChoiceField labels