[英]After Ajax call ,django not redirect given url?
I want to put loading image while saving data in database. 我想在将数据保存到数据库时放入图像。 So ,I Ajax call to do it.
所以,我Ajax打电话去做。 I use Django 1.8 and after put Ajax call Django not redirect default page after adding data successfully.I check data in save successfully and view.py method also run.
我使用Django 1.8,并在成功添加数据后将Ajax调用Django不重定向默认页面。我成功检查了保存中的数据并运行了view.py方法。
Before put Ajax call when I submit invalid data to forum and when submit it ,show validation errors.but now only view Ajax error message. 在我向论坛提交无效数据以及提交无效数据之前,请先进行Ajax调用,然后显示验证错误。但现在仅查看Ajax错误消息。
but after add ajax , now I enter submit without data didn't show above instruction(validation error). 但是添加ajax之后,现在我输入没有数据的提交没有显示以上指令(验证错误)。 I used Django default validation an they are defined model class when create form.
我使用了Django默认验证,当创建表单时它们是定义的模型类。
----------------keylist.html------------------------------ ---------------- keylist.html ------------------------------
{ % extends "base.html" %}
{% load crispy_forms_tags %}
{% block title %}
{% if create %}Create{% else %}New Key{% endif %}Serious
{% endblock %}
{% block heading %}
<h2>
Create New Serial Keys
</h2>
{% endblock %}
{% block content %}
{% if create %}
{% url "marcador_key_create" as action_url %}
{% else %}
{% url "marcador_bookmark_search" pk=form.instance.pk as action_url %}
{% endif %}
<form action="{{ action_url }}" method="post" accept-charset="utf-8" id="createForm" >
{{ form|crispy }}
{% csrf_token %}
<div id="submit_loader"></div>
<p> <b>Expiry Date*:</b> <input type="date" id="datepicker" name="expier_date"></p>
<p><input type="submit" class="btn btn-default" value="Save" id="save_button"> </p>
</form>
{% endblock %}
-------base.html (ajax method )------------------------------------- ------- base.html(ajax方法)------------------------------------ --
<script>
// Attach a submit handler to the form
$("#createForm").submit(function (event) {
event.preventDefault();
//var formData = new FormData($("#createForm")[0]);
var serverUrl =$("#createForm").attr('action');
$.ajax({
url: serverUrl,
type: 'POST',
data: $("#createForm").serialize(),
beforeSend: function() {
$('#submit_loader').css('display','block');
},
complete: function(){
$('#submit_loader').css('display','none');
},
success: function (returndata) {
alert("succsesfully generate keys");
//window.location("xsd");
//return false;
},
error: function(data){
alert("please ,check you fill form correctly");
}
});
//return false;
});
</script>
---------view.py (calling method )---------------------------------- --------- view.py(调用方法)----------------------------------
@login_required
def key_create(request):
#print(request.POST)
if request.method == 'POST':
form = KeyGenarateForm(data=request.POST)
expier_date = request.POST['expier_date']
#print(expier_date=="")
if(expier_date==""):
form.is_valid= False;
if form.is_valid():
#request.POST._mutable = True
Key_Gen = form.save(commit=False)
Key_Gen.save(expier_date)
return redirect('marcador_bookmark_user',username=request.user.username)
else:
print('form not valied')
return render('marcador_bookmark_user',username=request.user.username)
else:
form = KeyGenarateForm()
context = {'form': form, 'create_key': True}
return render(request, 'marcador/key_genarate_form.html', context)
I test when I enter valid data and submit , every thing successfully , but didn't redirect my url.It show my old data in the field. 我测试了何时输入有效数据并成功提交了所有东西,但没有重定向我的URL,而是在字段中显示了我的旧数据。
As,I noticed view.py return method not loading. 作为,我注意到view.py返回方法未加载。
return redirect('marcador_bookmark_user',username=request.user.username)
not execute. 不执行。
please , expect some expert help. 请,期待一些专家的帮助。
May be this will help you: 可能这会帮助您:
Instead of this redirection in views.py: 而不是在views.py中重定向:
return redirect('marcador_bookmark_user',username=request.user.username)
use this: 用这个:
return HttpResponse(json.dumps([{username=request.user.username}]),mimetype='text/json')
At last on ajax success function: 最后关于ajax的成功功能:
window.location.href = '/url-path'+data.username;
(username will be a key inside the context named "data"). (用户名将是名为“ data”的上下文中的键)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.