简体   繁体   English

管理员注册人员无法登录Django admin,表单注册人员无法登录

[英]admin registered staff user can't login django admin, form registered staff user can't

When I create a user via django admin panel, and mark it staff it can login django admin page as it should, but when I create user vie UserForm and mark it as staff it can not login. 当我通过django管理面板创建用户并将其标记为人员时,可以按需登录django管理页面,但是当我创建用户vie UserForm并将其标记为人员时,则无法登录。 It says invalid username password for staff and so on. 它说职员的用户名密码无效,依此类推。

I suspect the error is because I am using, 我怀疑错误是因为我正在使用,

TEMPLATE_CONTEXT_PROCESSORS = ('django.contrib.auth.context_processors.auth','django.core.context_processors.request')

Currently I have the following code: 目前,我有以下代码:

user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)

if user_form.is_valid() and profile_form.is_valid():
    user = user_form.save()
    user.set_password(user.password)
    user.save()

As form: 形式如下:

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model = User
        fields = ('username', 'email', 'password')

Now tell me, how can I by pass this problem? 现在告诉我,如何解决这个问题?

three simple step: 三个简单的步骤:

  1. create user: 创建用户:

user = User.objects.create_user("username")

  1. set password for your user: 为您的用户设置密码:

    user.password = "your_password"

    user.set_password(user.password)

  2. make user as super user: 使用户成为超级用户:

user.is_staff=True

user.is_superuser=True'

  1. save your user: 保存用户:

    user.save()

now you can login in admin panel of django 现在您可以登录django的管理面板

EDIT : instead of all above you can do just: 编辑 :而不是以上所有您可以做:

python manage.py createsuperuser

and create a super user 并创建一个超级用户

phew, I had to add something like this: ,我不得不添加这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM