用户提交后,我将用户在表单中提交的数据重定向回同一页面。我可以看到数据POST,然后紧接着在Firefox 36.0.4桌面,Chrome 41.0.2272桌面,Opera 10.0 iPhone 5,Chrome 41.0.2272 iPhone 5中同一页面的GET。但是,只有移动设备上的Chrome导致刷新时“确认重新提交”,并且不会再次加载所有js。这个问题有解决方法吗?
这是我的简单简单视图。
def workout(request):
form = WorkoutInfoForm(request.POST or None)
if form.is_valid():
form.save()
post = request.POST.copy()
request.session['_old_post'] = post
return redirect(reverse("workout.views.workout"))
if not request.POST:
old_post = request.session.get('_old_post')
form = WorkoutInfoForm(old_post or None)
return render_to_response("workout/track_workout.html",
locals(),
context_instance=RequestContext(request))
我的表单从我的模板开始
<form method='POST' action='' class="form-horizontal" role="form"> {% csrf_token %}
答案 0 :(得分:1)
如果表单有效,您的代码将按预期工作。仅在表单无效的情况下才会出现所述问题。你可以试试像
这样的东西def workout(request):
form = WorkoutInfoForm(request.POST or None)
if request.POST:
post = request.POST.copy()
request.session['_old_post'] = post
if form.is_valid():
form.save()
return redirect(reverse("workout.views.workout"))
else:
old_post = request.session.get('_old_post')
form = WorkoutInfoForm(old_post or None)
return render_to_response("workout/track_workout.html",
locals(),
context_instance=RequestContext(request))