Skip to content

Instantly share code, notes, and snippets.

@qqpann
Created March 19, 2019 01:49
Show Gist options
  • Save qqpann/1a358f766de4dfec8cbe10635e70490d to your computer and use it in GitHub Desktop.
Save qqpann/1a358f766de4dfec8cbe10635e70490d to your computer and use it in GitHub Desktop.
[Django image upload_to uuid filename snipet] #django
import uuid, os, base64
def uuid32():
return base64.b32encode(uuid.uuid4().bytes).decode('ASCII').rstrip('=')
def get_filefield_ascii_uuid(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
prefix = 'image'
name = uuid32()
extension = os.path.splitext(filename)[-1]
return '{0}/{1}{2}'.format(prefix, name, extension)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment