Django file upload – How to check if filename already exists in disk

Use this solution to raise an error when the uploaded file has the same name as a previously stored file in system.

In the following code, the FileField is named my_file. The clean_my_file method ensures that the user will be informed if the filename already exists in the destination directory and will prevent the user from saving the file.

Bruno Bastos

I'm a developer specialized in building web applications, focused on applying productive habits, willing to help and learn from this blog.

  • Martin Hammer

    I use os.path.exists(file) instead.

  • Safari Mupe

    I know it’s a while and I am late but what means