我可能会向后看这个但是我试图在成功函数中使用pk来创建一个href来加载。
pk将是新的并且已由save()创建 我想知道的是如何将self.pk包发布到js以使用它。 javascript
JS
$.ajax({
type: "POST",
url: "/api/add/res/",
data: JSON.stringify(data),
success: function(response) {
var successMessage = $('<div>').text('New Personnel file created in database...').css({'color':'green', 'padding':'5px'}).addClass('pull-right');
$('.form-group').removeClass('has-error')
$('#submitresource').html('Added!').addClass('btn-success')
$('.modal-row' ).append(successMessage);
$.get('newres_pk'),
window.setTimeout(function(){window.location.href ="'/Personnel/results.html?id="+newres_pk+"'"},3000)
},
contentType: "application/json",
dataType: "json"
})
def set(request):
data = json.loads(request.body)
res = Resource.objects.create(
title=data['title'],
preferred_name=data['preferred_name'],
last_name=data['last_name'],
employstatus=data['employstatus'],
employer=Employer.objects.get(employer_name=data['employer']),
role=Role.objects.get(role_name=data['role']),
location=Location.objects.get(name=data['location']),
workphone=data['workphone'],
mobile_phone=data['mobile_phone'],
email=data['email'],
notes=data['notes'],
updated_by=data['updated_by'],
)
res.save()
newres_pk = res.pk
print res
return HttpResponse('"Submitted"', content_type='application/json')
答案 0 :(得分:1)
返回时,您可以在ex:
中包含您要发送的数据from django.http import JsonResponse
data = {'message':'your message'}
return JsonResponse(data)
并在javascript ..
success: function(response) {
console.log(response.message);