经过将近一整天的努力工作,我辞职......我需要一些帮助,因为我不明白我在哪里做错事。 我搜索了SO并偶然发现了许多关于重定向等问题的答案......
让我给你一些背景信息: 我有一个简单的表单,可以上传文件,到目前为止,它运行良好。我希望在上传成功后将用户重定向到另一个页面,这就是失败的地方:'(
我的views.py:
# Create your views here.
def thank_you(request):
data = {'text': 'Thank you for your file'}
print('thank you blablabla')
print(data)
return render(request, 'app/thank_you.html', data)
def home(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
uploaded_file = request.FILES['file']
new_file = UploadFile(file=uploaded_file)
new_file.save()
return redirect(reverse(thank_you))
else:
form = UploadFileForm()
data = {'form': form}
return render(request, 'app/drop_file.html', data)
我确实在' thank_you'中看到了2张照片。功能,意味着重定向按预期工作。但是这种观点并没有刷新,而且我被困住了。 如果我尝试直接访问网址(转到http://.../thank_you/),它确实显示正确。
thank_you.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload done</title>
</head>
<body>
<h1>Hello !</h1>
<h1>{{ text }} </h1>
</body>
</html>
表单看起来像这样:
<form id="my-dropzone" class="dropzone" action="{% url 'home' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
</form>
<button id="submit-all">
Submit all files
</button>
<script type="text/javascript">
Dropzone.options.myDropzone = {
// Prevents Dropzone from uploading dropped files immediately
autoProcessQueue : false,
init : function() {
var submitButton = document.querySelector("#submit-all")
myDropzone = this;
submitButton.addEventListener("click", function() {
myDropzone.processQueue();
// Tell Dropzone to process all queued files.
});
// You might want to show the submit button only when
// files are dropped here:
this.on("addedfile", function() {
// Show submit button here and/or inform user to click it.
});
this.on("queuecomplete", function() {
console.log('should we redirect ?');
});
}
};
</script>
我的urls.py文件:
url(r'^$', views.home, name='home'),
url(r'thank_you/$', views.thank_you, name='thank_you'),
我猜没有什么不寻常的。
即使我改变了家庭&#39;致:
if form.is_valid():
uploaded_file = request.FILES['file']
new_file = UploadFile(file=uploaded_file)
new_file.save()
data = {'text': 'Thank you for your file'}
print('thank you blablabla')
print(data)
return render(request, 'app/thank_you.html', data)
视图没有更新,我确实在django控制台中看到了打印但模板没有渲染......
我希望return render(request, 'app/thank_you.html', data)
在提交表单后实际刷新页面,但我无法实现这样做:(有任何帮助吗?
Firebug在控制台中向我显示: console output