0π
β
I solved the problem now.
def key_download(request, username):
username_key = username +".key"
fsock = open('/var/www/openvpn/examples/easy-rsa/2.0/keys/'+username_key, 'r')
response = HttpResponse(fsock, mimetype='application/pgp-keys')
response['Content-Disposition'] = "attachment; filename = %s " % (username_key)
return response
A cool tip for the guys who might want to find out the mimetype
>>> import urllib, mimetypes
>>> url = urllib.pathname2url(filename)
>>> print mimetypes.guess_type(url)
π€BoJack Horseman
1π
You also need to set the HTTP header
of the response to something like application/force-download
. Please see docs.
See also this question.
π€0xc0de
- [Answer]-Dynamically add fields to a ModelForm in Django
- [Answer]-Unable to save model instance with django
0π
/var/www
is not a URL. Itβs a file path on your server. If you want users to access a file in that directory, you have to configure Apache (or whatever) to serve it at a particular address, and then use that URL in your template rather than the file path.
π€Daniel Roseman
- [Answer]-Django Nested ManyToMany QuerySet
- [Answer]-How Do I Get Unique IDs Efficiently in Python?
- [Answer]-Django: capturing values in url to generic class view
- [Answer]-What kind of response should I return from a simple django AJAX view?
- [Answer]-Truncatewords by percentage of total words count
Source:stackexchange.com