我正在尝试使用angular $ http帖子将数据传递到django视图,但它没有发生。每次我传递数据时都显示空dict,这意味着我没有正确使用POST请求。我是集成django和angular app的新手,所以如果有人可以帮我解决这个问题会很棒。
app.js
vm.login = function() {
vm.authMsg = '';
var login_data = {'username': vm.account.username, 'password': vm.account.password};
if(vm.loginForm.$valid) {
$http
.post('/login_app/', {login_data:JSON.stringify(login_data)})
.then(function(response) {
console.log(login_data);
if ( !response.login_data ) {
vm.authMsg = 'Incorrect credentials.';
// vm.authMsg = response;
}else{
$state.go('app.dashboard');
}
}, function() {
vm.authMsg = 'Server Request Error';
});
}
else {
vm.loginForm.account_username.$dirty = true;
vm.loginForm.account_password.$dirty = true;
}
};
views.py
def login_app(request):
if request.method == 'POST':
print request.POST
username = request.POST.get('username')
password = request.POST.get('password')
print 'username', username
print 'password', password
user = authenticate(username=username, password=password)
print 'user', user
if user:
if user.is_authenticated():
print 'user authenticate'
login(request, user)
return HttpResponse(json.dumps({}), content_type='application/json')
else:
return HttpResponse('<h1>Access Denied.<br>Wrong Credentials.</h1>')
的login.html
<form role="form" ng-submit="login.login()" name="login.loginForm" novalidate="" class="form-validate mb-lg" method="post">
<div class="form-group has-feedback">
<input id="exampleInputUsername1" type="text" name="account_username" placeholder="Enter username" autocomplete="off" ng-model="login.account.username" required="" class="form-control" />
<span class="fa fa-envelope form-control-feedback text-muted"></span>
<span ng-show="login.loginForm.account_username.$dirty && login.loginForm.account_username.$error.required" class="text-danger">This field is required</span>
</div>
<div class="form-group has-feedback">
<input id="exampleInputPassword1" type="password" name="account_password" placeholder="Password" ng-model="login.account.password" required="" class="form-control" />
<span class="fa fa-lock form-control-feedback text-muted"></span>
<span ng-show="login.loginForm.account_password.$dirty && login.loginForm.account_password.$error.required" class="text-danger">This field is required</span>
</div>
<div class="clearfix">
<div class="checkbox c-checkbox pull-left mt0">
<label>
<input type="checkbox" value="" name="account_remember" ng-model="login.account.remember" />
<span class="fa fa-check"></span>Remember Me</label>
</div>
<div class="pull-right"><a ui-sref="page.recover" class="text-muted">Forgot your password?</a>
</div>
</div>
<button type="submit" class="btn btn-block btn-primary mt-lg">Login</button>
</form>
urls.py
from django.conf.urls import patterns, url
from login import views
urlpatterns = patterns('',
url(r'^$',views.home, name='home'),
url(r'^login_app/$',views.login_app, name='login_app'),
)
答案 0 :(得分:2)
试试这个。
如果你没有包装/ jsonify login_data,你的现有视图应该能够解压缩它。我非常确定棱角自动处理json转换,但我不认为django会这样做。
vm.login = function() {
vm.authMsg = '';
var login_data = {'username': vm.account.username, 'password': vm.account.password};
if(vm.loginForm.$valid) {
$http
.post('/login_app/', login_data)
.then(function(response) {
console.log(login_data);
[...]
看起来您还想查看request.body
以获取您的json数据。如果你在django&lt; 1.4,它有所不同。
def login_app(request):
if request.method == 'POST':
print request.body
data = json.loads(request.body)
print data
username = data.get('username')
password = data.get('password')
[...]