Django:按下按钮时提供动态生成的数据作为附件
问题概述:
我正在创建一个基于 Django 的客户端,旨在从 Web 服务返回数据。该项目的目标是根据用户在表单中选择的值从 Web 服务将数据返回给用户。表单提交后,会生成一个查询字符串,发送到 Web 服务,并以字符串形式返回页面数据。目前,该数据在浏览器中显示给用户。我想提供允许用户单击按钮并下载数据的功能。
问题:
当用户单击浏览器中的下载按钮时,如何将数据返回给用户?如何使相同数据的不同选项可用(即 application/json 或 text/csv)?
当前(不起作用)实现:
我正在尝试执行以下操作,但失败了:
views.py
返回模板的 render_to_response 对象。我将表单以及各种表单中的数据传递给模板。
def view(request):
#Do stuff, get data as string
#Get data into needed formats (see utils.py)
jsonData = jsonToJsonFile(dataString, fileName)
return render_to_response('template.html', {'someForm' : aForm,
'regularData' : stringData,
'jsonData' : jsonData...})
utils.py
包含将数据作为字符串并返回响应对象的函数。这部分我不确定我是否做得正确。我在视图中调用这些函数,以将 jsonData (和 csvData)从原始数据字符串转换为正确的格式。
def jsonToJsonFile(dataString, fileName):
#Get the data as json
theData = json.dumps(dataString)
#Prepare to return a json file
response = HttpResponse(theData, mimetype = 'application/json')
response['Content-Disposition'] = 'attachment; filename=' + str(fileName) + '.json'
#return the response
return response
template.html
我目前正在将响应传递到模板中。这就是我真正迷失的地方,还没有开始找到好的解决办法。我希望在单击按钮时需要使用 javascript 将变量(jsonData 和 csvData)返回给用户。我尝试使用锚类的 onclick 操作,然后使用 javascript 返回响应的 django 变量 - 但这确实不起作用。
<li class = 'button'>
<a href = "#dataButtons" onclick = "javaScript:alert('test');">
TEST
</a>
</li>
<li class = 'button'>
<a href = "#dataButtons" onclick = "javaScript: var a = '{{ jsonData }}'; return a;">
JSON
</a>
</li>
我将测试部分放在那里,测试警报是否有效。确实如此。但是,当我单击 json 数据的按钮时,没有任何反应。
我的做法完全错误吗?或者我缺少什么小东西吗?
Problem Overview:
I am creating a Django-based client with the intent of returning data from a web service. The goal of this project is to return data to the user from the web service based on the values selected by the user in a form. Upon the form submit, a query string is generated, sent to the web service and the page data is returned as a string. Currently, that data is displayed to the user in the browser. I want to provide the functionality that would allow the user to click a button and download the data.
Question:
How could I return the data to the user when they click a button in their browser for download? How do I make different options of the same data available (i.e. application/json, or text/csv)?
Current (not-working) Implementation:
I am attempting, and failing, to do the following:
views.py
Returning a render_to_response object of my template. To the template I pass the form, and the data in it's various forms.
def view(request):
#Do stuff, get data as string
#Get data into needed formats (see utils.py)
jsonData = jsonToJsonFile(dataString, fileName)
return render_to_response('template.html', {'someForm' : aForm,
'regularData' : stringData,
'jsonData' : jsonData...})
utils.py
Contains functions to take the data as a string and return response objects. This part I am unsure if I am doing correctly. I call these functions in the view to get jsonData (and csvData) into their proper formats from the original data string.
def jsonToJsonFile(dataString, fileName):
#Get the data as json
theData = json.dumps(dataString)
#Prepare to return a json file
response = HttpResponse(theData, mimetype = 'application/json')
response['Content-Disposition'] = 'attachment; filename=' + str(fileName) + '.json'
#return the response
return response
template.html
I am currently passing the responses into the template. This is where I am really lost, and have not yet begun to find a good solution. I expect I will need to use javascript to return the variables (jsonData and csvData) to the user when the button is clicked. I have attempted the use of the onclick action of the anchor class, and then using javascript to return the django variable of the response - but this really isn't working.
<li class = 'button'>
<a href = "#dataButtons" onclick = "javaScript:alert('test');">
TEST
</a>
</li>
<li class = 'button'>
<a href = "#dataButtons" onclick = "javaScript: var a = '{{ jsonData }}'; return a;">
JSON
</a>
</li>
I put the test part in there to, well, test whether or no the alert would work. It does. However, when I click the button for the json data, nothing happens.
Am I approaching this completely wrong? Or is there something small that I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案:
在进一步研究问题并与同事讨论后,我的问题似乎在于尝试将响应对象传递给 JavaScript。对于那些感兴趣的人,我通过仔细地重新路由数据来解决了这个问题。
在我的views.py
中,我在主视图中添加了几行代码,这些代码将两个额外视图的变量(一个用于csv,一个用于json)设置为保存数据的响应对象。当按下各自的按钮时,将调用这两个额外的视图,返回 httpresponse 并提示用户下载。
template.html
然后,当用户按下按钮时,锚点将通过 url 链接到辅助 django 视图,该视图将向用户显示下载选项。
urls.py
最后,我只是将我的 url 模式指向视图。
到目前为止,这个方法对我来说非常有效!如果有人有任何其他建议或更好的方法,我当然愿意听取。
Solution:
After looking at the problem a little further and talking to a colleague about it, it seems as though my problem lies in trying to pass the response object to the javascript. For those interested, I solved the problem with a little careful re-routing of the data.
views.py
In my views.py I added a couple lines of code in my main view that would set variables of two extra views (one for csv one for json) to the response objects holding the data. These two extra views would then be called when their respective buttons were pressed, returning the httpresponse and prompting the user for download.
template.html
Then, when the button is pressed by the user, the anchor is linked via the url to the secondary django view that will present the download option to the user.
urls.py
Lastly, I just pointed my url patterns to the view.
So far, this method has worked great for me! If anyone has any other suggestions, or a better method, I would certainly be open to hear it.
我认为你需要更改你的 javascript 以使其启动文件下载 - 请参阅此问题 &回答:
使用 JavaScript 开始文件下载
I think you need to change your javascript to make it start a file download - see this question & answer:
starting file download with JavaScript