Skip to content

Instantly share code, notes, and snippets.

@BuildWithLal
Last active June 7, 2018 11:33
Show Gist options
  • Save BuildWithLal/b53641895fc6d87d30e44f350ede0eb2 to your computer and use it in GitHub Desktop.
Save BuildWithLal/b53641895fc6d87d30e44f350ede0eb2 to your computer and use it in GitHub Desktop.
# convert image and video fields to StringField() in models.py
# remove type field from models.py and forms.py as well
# view.py changes
# create_image view
image = form.save()
image_file = request.FILES['image']
image_type = request.FILES['image']['name'].split('.')[-1] # can be png, jpg, jpeg etc
image_name = f'{image.pk}.{image_type}'
with open(os.path.join(settings.MEDIA_ROOT, image_name)', 'wb+') as destination:
for chunk in image_file.chunks():
destination.write(chunk)
image.image = image_name
# video save
video_file = request.FILES['video']
video_type = request.FILES['video']['name'].split('.')[-1] # can be mp4, flv, mkv etc
video_name = f'{image.pk}.{video_type}'
with open(os.path.join(settings.MEDIA_ROOT, video_name)', 'wb+') as destination:
for chunk in video_file.chunks():
destination.write(chunk)
image.video = video_name
image.save()
# changes in template
<img src="/media/{{ image.image }}" />
<video source="/media/{{ image.video }}" />
#BOOOOOM.................
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment