I want to add user in a Django app. here’s my code, it fires an exception in this line
user = User.objects.create(userName, userMail, userPass)
Here’s the whole code:
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(userName, userMail, userPass)
user.save()
return render_to_response('home.html', context_instance=RequestContext(request))
Any help?
Use the
create_userhelper function instead of thecreatemethod. It takes care of hashing the password for you, amongst other things.As an aside, fetching the values out of the request data dictionary isn’t best practice. It’s a good idea to learn about django forms and model forms, which will validate input data for you. Once you understand that, there’s a
UserCreationFormincluded with the django auth app which you could use.