0

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?

Amr M. AbdulRahman
  • 1,820
  • 3
  • 14
  • 22

1 Answers1

4

Use the create_user helper function instead of the create method. It takes care of hashing the password for you, amongst other things.

user = User.objects.create_user(userName, userMail, userPass)

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 UserCreationForm included with the django auth app which you could use.

Alasdair
  • 298,606
  • 55
  • 578
  • 516