Angularjs ValueError:必须设置给定的用户名
我正在尝试在我的项目中注册用户,但当我尝试注册时,会出现下一个错误: ValueError:必须设置给定的用户名 在我的项目中,我使用Django 1.11和AngularJS 1.6 View.pyAngularjs ValueError:必须设置给定的用户名,angularjs,django,django-models,Angularjs,Django,Django Models,我正在尝试在我的项目中注册用户,但当我尝试注册时,会出现下一个错误: ValueError:必须设置给定的用户名 在我的项目中,我使用Django 1.11和AngularJS 1.6 View.py def registrarCliente(request): if request.method=='POST': usuariosfinales=[] nombre=request.POST.get('nombre') email=requ
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>
调试页面告诉我我的错误在:
user=user.objects.create\u用户(电子邮件、电子邮件、密码)
但我不明白为什么。请放松点
user = User(email = email, username= email, password =password)
user.save()
只需确保名称与您使用的型号相同
不要让它难以使用我发送的数据如下所示:
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
(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);
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';
}
);
}
}])
正如您在发布的图片中所看到的,在localvars部分中,所有变量(如email、nombre和password)都是零。试着打印这些变量,看看你的视图是否正确地接收到POST参数,或者看看你的javascript代码是否正确地发送了这些变量。@rayy我想你可能有理由。当地的VAR没有!但是,js和py之间的连接在上面,我正在查找错误,没有找到任何错误。是的,用户与Author的用户模型同名。如果有人知道如何在不使用jquery的情况下进行此操作,请对此进行评论!