Amr M. AbdulRahman Amr M. AbdulRahman - 2 years ago 83
Python Question

How to create an user in 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)**

return render_to_response('home.html', context_instance=RequestContext(request))

Any help?

Answer Source

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',
                                 password='glass onion')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download