I’m trying to create a new User in a Django project by the following code, but the highlighted line fires an exception.
JavaScript
x
12
12
1
def createUser(request):
2
userName = request.REQUEST.get('username', None)
3
userPass = request.REQUEST.get('password', None)
4
userMail = request.REQUEST.get('email', None)
5
6
# TODO: check if already existed
7
8
**user = User.objects.create_user(userName, userMail, userPass)**
9
user.save()
10
11
return render_to_response('home.html', context_instance=RequestContext(request))
12
Any help?
Advertisement
Answer
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..
JavaScript
1
5
1
from django.contrib.auth.models import User
2
user = User.objects.create_user(username='john',
3
email='jlennon@beatles.com',
4
password='glass onion')
5