[Django]-How to modify django ImageField to accept svg files

2👍

So after browsing the forum I came across the solution:

validators.py

import os
from django.core.exceptions import ValidationError

def validate_file_extension(value):
    ext = os.path.splitext(value.name)[1]  # [0] returns path+filename
    valid_extensions = ['.jpg', '.jpeg', '.png', '.svg']
    if not ext.lower() in valid_extensions:
        raise ValidationError('Unsupported file extension.')

models.py

from app_name.valiators import validate_file_extension

image = models.ImageField('Image', upload_to=image_upload_path, validators=[validate_file_extension])

Leave a comment