2015-05-13 124 views
0

我正在編寫一個API,並且註冊時出現問題:ValueError : The given username should be set在django上註冊

下面是代碼:

def create_account(request): 
    if request.method == 'POST': 
     username = request.POST.get('username', '') 
     email = request.POST.get('email', '') 
     check_username = User.objects.filter(username__exact=username) 
     check_email = User.objects.filter(email__exact=email) 

     if check_email.count() != 0: 
      if check_username.count() != 0: 
       check = json.dumps('User with such name or email already registered. Please, rewrite your name or email.') 
      return HttpResponse(check, content_type='application/json') 

     password = request.POST.get('password', '') 
     password2 = request.POST.get('password2', '') 

     if password != password2: 
      wrong_password_message = json.dumps('The passwords do not match. Please, rewrite!') 
      return HttpResponse(wrong_password_message, content_type='application/json') 
     else: 
      user = User.objects.create_user(username, email, password) 
      user.is_active = False 
      user.is_staff = False 
      user.is_superuser = False 
      user.save() 
    return HttpResponse(json.dumps('Hello new User!'), content_type='application/json') 

回答

1

改變這一行:

user = User.objects.create_user(username, email, password)` 

user = User.objects.create_user(username=username, email=email, password=password)` 

這應該工作。

0

paradoksumsu的答案沒有幫助,儘管他從函數'create_user'的語法角度說得很對。

要修正這個錯誤,你應該做到以下幾點:

- 更改所有request.POST.get('smth')request.META[HTTP_SMTH]

爲即:

username = request.META['HTTP_USERNAME'] 
email = request.META['HTTP_EMAIL'] 

它的工作原理。