【发布时间】:2011-10-01 06:11:46
【问题描述】:
我正在使用 Django。我有一个 HTML 页面,我在其中做一些 Javascript 的东西,然后我用这种方式做一个 jQuery 帖子:
$.ajax({
url: '/xenopatients/measurement/qual',
type: 'POST',
data: {'obj':data},
dataType: 'json',
contentType: "application/json; charset=utf-8", //questo ok
});
在这个 post 请求之后,我的 Django 视图正确地处理了对该 URL 的调用。 我想要它做的是处理数据,将用户发送到另一个页面,然后将此数据发送到新页面。 问题是我不能像往常一样在 Python 中执行重定向,就像代码忽略了重定向一样。
我的 Python 代码是:
@csrf_protect
@login_required#(login_url='/xenopatients/login/')
def qualMeasure(request):
name = request.user.username
print "enter"
if request.method == 'POST':
print request.POST
if "obj" in request.POST:
print 'obj received'
return render_to_response('mice/mice_status.html', RequestContext(request))
return render_to_response('measure/qual.html', {'name': name, 'form': QualMeasureForm()}, RequestContext(request))
我发现更改页面的唯一方法是在上面的代码之后通过 Javascript:
top.location.href = "/xenopatients/measurement";
但是我不知道如何在使用这个方法时传递我需要的数据。
HTML 代码:
<form action="" method="">
<table id="dataTable" width="100%" border="1"></table><br>
<script language="javascript">
document.measureForm.id_barcode.focus();
document.measureForm.Add.disabled = false;
$('#dataTable').tablePagination({});
</script>
<input type="button" name="save" value="Save Measure Serie" onclick="table2JSON('dataTable')"/>
</form>
附:我也试过$.post,但结果相同。
如何在 Django 中使用 jQuery 进行发布请求后进行重定向?
【问题讨论】:
-
如果你想重定向,你为什么用 Ajax 做呢?为什么不以正常方式提交表单?使用 Ajax 的主要原因是避免重定向。
-
我使用 ajax 来动态更改页面数据并将复杂数据发送到服务器......而且我不是独一无二的! ;)
-
python代码的可读性不是很好,因为标识有问题...
-
@acidjunk 谢谢,我从未见过那个错误。现在我已经修好了。 :)
标签: jquery ajax django post redirect