12👍
Put something like this in your settings
#Authentication backends
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
or if you are using userena for your accounts
#Authentication backends
AUTHENTICATION_BACKENDS = (
'userena.backends.UserenaAuthenticationBackend',
'guardian.backends.ObjectPermissionBackend',
'django.contrib.auth.backends.ModelBackend',
)
9👍
Interestingly enough, check_password returns True in the following:
eml = "4@a.com"
pw = "pass"
uname = 'w2'
user = User.objects.create_user(uname,eml,pw)
user.save()
log.debug("Password check passes?")
log.debug(user.check_password(pw)) # Logs True!!!
user = authenticate(username=uname, password=pw)
- [Django]-Django: get table name of a model in the model manager?
- [Django]-Django reverse causes circular import
- [Django]-ImportError: cannot import name 'url' from 'django.conf.urls' after upgrading to Django 4.0
4👍
Why don’t you create a user like this:
user = User.objects.create_user( username="whatever", email="whatever@some.com", password="password")
user = authenticate( username="whatever",password="password")
- [Django]-Django – Why should I ever use the render_to_response at all?
- [Django]-Django 1.8: Create initial migrations for existing schema
- [Django]-How to return an rest_framework.response object from a django custom middleware class?
3👍
In settings.py, add
AUTH_USER_MODEL = your custom user class
e.g if django app name is office and custom user class is Account then
AUTH_USER_MODEL = 'office.Account'
- [Django]-Can't install Django 2.0 by pip
- [Django]-Parentheses in django if statement
- [Django]-Get model's fields in Django
2👍
set_password
is a misleading method, it doesn’t save the password on the user table. You need to call user.save()
in order for it to work on your flow
- [Django]-How to use the 'reverse' of a Django ManyToMany relationship?
- [Django]-How to install libpq-fe.h?
- [Django]-Django: construct value of {% include %} tag dynamically?
1👍
You have to check whether user is active? If not, you only set active for user in admin panel, or set when creating user by adding the following line to user model:
is_active = models.BooleanField(default=True)
- [Django]-Django {% with %} tags within {% if %} {% else %} tags?
- [Django]-Django access the length of a list within a template
- [Django]-PermissionError with pip3
1👍
I puzzled with this problem for four days, and the above answers didn’t help.
The problem was that I, as you, was using the user.save() and I could not see what the problem was, but then I looked at it with debug eyes, and it turns out that if you use user.save() it messes with the hashing of the password somehow, don’t ask me how I don’t know. So I worked around it by using the user.create() method that Django provides, worked like a charm:
@api_view(['POST'])
def create_user(request):
new_user = UserSerializer(data=request.data)
if new_user.is_valid():
user_saved = new_user.create(request.data)
return Response('User {} created'.format(user_saved.username),
status=status.HTTP_200_OK)
else:
return Response('User not created', status=status.HTTP_200_OK)
I used something like this, but you can do as you wish just use the user.create().
- [Django]-Check_password() from a user again
- [Django]-How to show processing animation / spinner during ajax request?
- [Django]-Django 1.11 Annotating a Subquery Aggregate
0👍
Also check that you have the right username/password combo. sometimes the one that is created from the createsuperuser command is different than a username you would typically use.
- [Django]-Django: Rest Framework authenticate header
- [Django]-Django values_list vs values
- [Django]-How to test Django's UpdateView?
0👍
As most of them suggested if we create the user’s using User.objects.create_user(**validated_data)
this will hash the raw password and store the hashed password. In-case if you you are using User model serializers
to validate and create users, it is required to override the serializer method like this
class UserSerializers(serializers.ModelSerializer):
class Meta:
model = User
fields = "__all__"
# this is the method responsible for insertion of data with hashed password
def create(self, validated_data):
return User.objects.create_user(**validated_data)
- [Django]-Django form: what is the best way to modify posted data before validating?
- [Django]-How to clear the whole cache when using django's page_cache decorator?
- [Django]-How can I MODIFY django to create "view" permission?