admin registered staff user can't login django admin, form registered staff user can't

2k views Asked by At

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. 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?

2

There are 2 answers

0
sadaf2605 On BEST ANSWER

phew, I had to add something like this:

AUTHENTICATION_BACKENDS = (
        'django.contrib.auth.backends.ModelBackend',
    )
0
Ali dashti On

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

EDIT: instead of all above you can do just:

python manage.py createsuperuser

and create a super user