Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a user in Django?

Tags:

python

django

I'm trying to create a new User in a Django project by the following code, but the highlighted line fires an exception.

def createUser(request):     userName = request.REQUEST.get('username', None)     userPass = request.REQUEST.get('password', None)     userMail = request.REQUEST.get('email', None)      # TODO: check if already existed      **user = User.objects.create_user(userName, userMail, userPass)**     user.save()      return render_to_response('home.html', context_instance=RequestContext(request)) 

Any help?

like image 296
Amr M. AbdulRahman Avatar asked Apr 29 '12 14:04

Amr M. AbdulRahman


People also ask

How do I login as user in Django?

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...

What is user model in Django?

For Django's default user model, the user identifier is the username, for custom user models it is the field specified by USERNAME_FIELD (see Customizing Users and authentication). It also handles the default permissions model as defined for User and PermissionsMixin .


1 Answers

The correct way to create a user in Django is to use the create_user function. This will handle the hashing of the password, etc..

from django.contrib.auth.models import User user = User.objects.create_user(username='john',                                  email='[email protected]',                                  password='glass onion') 
like image 75
keithhackbarth Avatar answered Oct 09 '22 22:10

keithhackbarth