我正在尝试在我的项目中注册用户,但是当我尝试这样做时,我会收到下一个错误:
ValueError:必须设置给定的用户名
在我的项目中,我使用的是Django 1.11和AngularJS 1.6
View.py
def registrarCliente(request):
if request.method=='POST':
usuariosfinales=[]
nombre=request.POST.get('nombre')
email=request.POST.get('email')
password=request.POST.get('password')
user=User.objects.create_user(email, email, password)
user.save()
usermodelo=Usuario(usuario=user, nombre=nombre)
usermodelo.save()
userfinal=authenticate(username=email, password=password)
login(request, userfinal)
usuariosfinales.append(userfinal)
data=serializers.serialize('json', usuariosfinales)
return HttpResponse(data, content_type="application/json")
return render (request, "login/login.html")
的login.html
<div class="login-wrap" id="login" ng-app="appLogin" ng-controller="conLogin">
<div class="login-right striped-bg" ng-show="Registro">
<div class="login-form">
<div class="heading">
¡Registrate!
</div>
<div class="input">
{% csrf_token %}
<div class="login-input-group spacer-field">
<span class="login-input-group-addon">
<i class="fa fa-user fa-fw"></i>
</span>
<input type="text" class="form-control" name="nombre" placeholder="Nombre" ng-model="user.nombre">
</div>
<div class="login-input-group spacer-field">
<span class="login-input-group-addon">
<i class="fa fa-at fa-fw"></i>
</span>
<input type="email" class="form-control" name="email" placeholder="Correo electrónico" ng-model="user.email">
</div>
<div class="login-input-group spacer-field">
<span class="login-input-group-addon">
<i class="fa fa-key fa-fw"></i>
</span>
<input type="password" class="form-control" name="password" placeholder="Contraseña" ng-model="user.password">
</div>
<div class="login-input-group">
<span class="login-input-group-addon">
<i class="fa fa-key fa-fw"></i>
</span>
<input type="password" class="form-control" name="password2" placeholder="Repetir contraseña">
</div>
<button class="btn btn-default btn-lg submit" type="submit" ng-click="registrar()">Registrarme</button>
</div>
</div>
</div>
</div>
appLogin.js
(function(angular) {
// body...
angular
.module('appLogin', [])
.controller('conLogin', ['$scope', '$http', function($scope, $http){
//Registro de usuario
$scope.user = {nombre:'', email:'', password:''};
$scope.registrar = function(email, password){
$http({method:'POST',
url:'/registro/',
data:{nombre: $scope.user.nombre, email: $scope.user.email, password: $scope.user.password}
}).then(
function(response2){
window.location.href='';
},function(response2){
$scope.user = response2.data || 'Request failed';
}
);
}
$scope.Ingreso = true;
$scope.Registro = false;
$scope.cambioRegistro = function(){
$scope.Ingreso = false;
$scope.Registro = true;
}
$scope.cambioIngreso = function(){
$scope.Ingreso = true;
$scope.Registro = false;
}
}])
var my_app = angular
.module('appLogin').config(
function($interpolateProvider, $httpProvider) {
$interpolateProvider.startSymbol('{$');
$interpolateProvider.endSymbol('$}');
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$httpProvider.defaults.withCredentials = true;
$httpProvider.defaults.cache = true;
}
);
})(window.angular);
调试页面告诉我我的错误是:
user = User.objects.create_user(电子邮件,电子邮件,密码)
但我不明白为什么。
答案 0 :(得分:1)
请放轻松!
user = User(email = email, username= email, password =password)
user.save()
只需确保您使用的名称为SAME AS型号。
请勿使用
答案 1 :(得分:0)
我发送的数据就像这样:
data:{nombre: $scope.user.nombre, email: $scope.user.email, password: $scope.user.password}
我没有使用$ .param jquery函数,因为我没有jquery,我导入了jquery,我用$ .param更改了上面的代码,就像这样:
data:$.param({nombre: $scope.user.nombre, email: $scope.user.email, password: $scope.user.password})
最后工作!
最终的AngularJS代码是:
appLogin.js
angular
.module('appLogin', [])
.controller('conLogin', ['$scope', '$http', function($scope, $http){
//Registro de usuario
$scope.user = {nombre:'', email:'', password:''};
$scope.registrar = function(email, password){
$http({method:'POST',
url:'/cliente/registro/',
data:$.param({nombre: $scope.user.nombre, email: $scope.user.email, password: $scope.user.password})
}).then(
function(response2){
window.location.href='';
},function(response2){
$scope.user = response2.data || 'Request failed';
}
);
}
}])