【发布时间】:2019-11-16 05:34:48
【问题描述】:
在我的 Django 项目中,用户将 Elasticsearch 查询提交到表单中,它会返回从该查询生成的可下载报告。我们进行了一些更改,现在我正试图让返回报告的部分再次工作。但是,我的 url 模式遇到了一个问题,应该调用 view 函数来下载报告。
我有一个Download Report 按钮,一旦生成报告(由 Ajax 请求检查),就会出现该按钮。这个想法是用户将单击该按钮,并且报告将出现在他们的下载文件夹中。但是,当我单击按钮时,它会将我发送到 /report/return_doc/ 而不是 /return_doc/。
将用户发送到/return_doc/ 的逻辑是它与我的视图中的return_doc 函数相关联,但是我可以触发该函数并将报告下载给用户而不刷新页面/将它们发送到新的url?还是我需要做一些完全不同的事情才能使这个按钮起作用?
错误信息
Page not found (404)
Request Method: GET
Request URL: http://0.0.0.0:0001/report/return_doc/
Using the URLconf defined in audit_tool_app.urls, Django tried these URL patterns, in this order:
admin/
accounts/
form/
report/ [name='form']
report/ ^static/(?P<path>.*)$
check_progress/ [name='check_progress']
return_doc/ [name='return_doc']
[name='home']
^static/(?P<path>.*)$
The current path, report/return_doc/, didn't match any of these.
audit_tool/urls.py
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.get_query, name='form'),
] + static(settings.STATIC_URL, document_root=settings.STAT)
audit_tool_app/urls.py
"""audit_tool_app URL Configuration"""
from django.contrib import admin
from django.urls import include, path
from django.views.generic.base import TemplateView
from django.conf import settings
from django.conf.urls.static import static
from audit_tool import views
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('form/', include('audit_tool.urls')),
path('report/', include('audit_tool.urls')),
path('check_progress/', views.check_progress, name='check_progress'),
path('report/return_doc/', views.return_doc, name='return_doc'),
path('', TemplateView.as_view(template_name='home.html'), name='home'),
] + static(settings.STATIC_URL, document_root=settings.STAT)
views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from docx import Document
import os
import threading
from .forms import QueryForm
from .models import *
import time
@login_required
def get_query(request):
if request.method == 'POST':
form = QueryForm(request.POST)
if form.is_valid():
query = form.cleaned_data["query"]
fn = "report_" + str(time.time()).replace(".", "_") + ".docx"
t = threading.Thread(target=generate_doc, args=(query, fn))
t.start()
return render(request, "audit_tool/check.html", {"fn": fn})
else:
return HttpResponse("Your query does not appear to be valid. Please enter a valid query and try again.")
else:
form = QueryForm()
return render(request, 'audit_tool/form_template.html', {'form': form})
@login_required
def check_progress(request):
"""
Returns status of document generation
"""
fn = request.POST["filename"]
file = "/app/created_files/" + fn
if not os.path.exists(file):
return JsonResponse({"report_in_progress": 1})
else:
return JsonResponse({"report_in_progress": 0})
@login_required
def return_doc(request):
"""
Returns report to user
"""
fn = request.POST["filename"]
file = "/app/created_files/" + fn
doc = Document(file)
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename={}'.format(fn)
doc.save(response)
return response
check.html
<!-- templates/django_audit/check.html -->
{% extends 'base_login.html' %}
{% block title %}Please wait{% endblock %}
{% load static %}
{% block content %}
<script type='text/javascript' src="{% static "bootstrap/js/jquery/1.7.1/jquery.min.js" %}"></script>
<script type="text/javascript">
$(document).ready( function() {
var fn = $('#fn').val()
var checkInterval = setInterval(isFileComplete, 3000); //3000 is 3 seconds
function isFileComplete() {
$.ajax({
url: '/check_progress/',
type: 'POST',
data: {
'filename': fn,
'csrfmiddlewaretoken': '{{ csrf_token }}',
},
dataType: 'json',
success: function (data) {
if (data.report_in_progress == 1) {
$("#download-button").hide();
} else {
$("#download-button").show();
clearInterval(checkInterval);
}
}
});
}
});
</script>
<p><br></p>
<p><br></p>
<div class="alert alert-primary" role="alert">
<p>Generating {{fn}}...please wait until the Download Report button appears.</p>
<button type="button" id="download-button" value="Download" onclick="window.open('return_doc')">Download Report</button>
</div>
<input id="fn" type=hidden value="{{fn}}">
{% endblock %}
【问题讨论】:
-
使用
anchor tag作为按钮怎么样?你试过吗? -
@Paolo 我试过
<a href="/return_doc/">Download Report</a>它给了我MultiValueDictKeyError at /report/return_doc/ \\ 'filename' \\ /app/icm_audit_tool/views.py in return_doc \\ 47. fn = request.POST["filename"] \\ /usr/local/lib/python3.7/site-packages/django/utils/datastructures.py in __getitem__ \\ 79. raise MultiValueDictKeyError(key) -
初始错误通过添加一个前导斜杠可以轻松修复:
onclick="window.open('/return_doc/')">Download- 但这只会导致与上述相同的错误,因为您实际上并没有发布文件名。 -
改用
request.POST.get('filename', None)。无论如何,按钮是否会在点击时发送任何数据? -
您似乎没有任何帖子数据
filename正在发送到您的视图。 @carousallie
标签: javascript python django django-urls