Skip to content

Instantly share code, notes, and snippets.

@Songbird0
Created November 19, 2017 20:40
Show Gist options
  • Save Songbird0/1c430b33dd0275fa2b4ef89f402d4224 to your computer and use it in GitHub Desktop.
Save Songbird0/1c430b33dd0275fa2b4ef89f402d4224 to your computer and use it in GitHub Desktop.
Django cheatsheet N°1
# Python version: 3.6.3
# See docs:
# * https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpResponse
# * https://docs.djangoproject.com/en/1.11/ref/request-response/#httprequest-objects
# * https://docs.python.org/3/library/io.html?highlight=io%20bytesio#io.BytesIO
def zipfile_gen(request: django.http.HttpRequest) -> django.http.HttpResponse:
import pathlib
import io
text_file = pathlib.Path(__file__).parent / 'a_text_file.txt'
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, 'w') as zipped_file:
zipped_file.write(text_file)
response = django.http.HttpResponse(buffer.getvalue(), content_type='application/x-zip-compressed')
response['Content-Disposition'] = 'attachment; filename=my_zip.zip'
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment