Reputation: 3102
I am reading a file in myView & passing it to myTemplate
myView
fp = open('D:/python1/format.pdf', 'rb')
data = fp.read()
response = HttpResponse(data, mimetype="application/pdf")
template = get_template('myTemplate.html')
variables = RequestContext(request, {'file':response})
output = template.render(variables)
return HttpResponse(output)
myTemplate
<object id="pdf" height="100%" width="100%" type="application/pdf" data="{{file}}">
Check this link.They have done a similar thing with csv file
Note:
I have done this successfully for an image file in zope (using python handler to read file and Dtml to put the raw image data in 'src'). Check this link.
and I know that Django shouldn't serve static files
.This is a special case.
Upvotes: 2
Views: 2891
Reputation: 31951
You can pass the data of the file as a string to the template as
fp = open('D:/python1/format.pdf', 'rb')
data = fp.read()
variables = RequestContext(request, {'file': data})
output = template.render(variables)
return HttpResponse(output)
Or you can serve it with web-server.
I don't think you can do something else. :-)
Upvotes: 1