Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How do I mock an uploaded file in django?

from django.core.files.uploadedfile import SimpleUploadedFile

# To mock a simple raw text file
mock_profile_image = SimpleUploadedFile('simple_file.txt', 'raw-text', content_type='text')

# To mock an image

# Just invoke this function and you'll get a png file
def get_mock_img(name='test.png', ext='png', size=(50, 50), color=(256, 0, 0)):
    file_obj = StringIO()
    image = Image.new("RGB", size=size, color=color)
    image.save(file_obj, ext)
    file_obj.seek(0)
    return File(file_obj, name=name)

mock_profile_image = SimpleUploadedFile('profile_img.png', get_mock_img(), content_type='image/png')

# or
with open(img_path, 'rb') as img_file:
	mock_profile_image = SimpleUploadedFile('profile_img.png', img_file.read(), content_type='image/png')
Comment

PREVIOUS NEXT
Code Example
Python :: convert string to unicode python 3 
Python :: how to append to every second item in list python 
Python :: RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() 
Python :: save pandas dataframe to parquet 
Python :: read json file python utf8 
Python :: plt line of best fit 
Python :: how to check if an input is a number in python 
Python :: PRINT VS RETURN IN PYTHON 
Python :: python add titles to subplots 
Python :: conver all dict keys to str python 
Python :: special characters list in python 
Python :: telegram markdown syntax 
Python :: how to convert a am pm string to 24 hrs time python 
Python :: django reverse 
Python :: python -m pip install --upgrade 
Python :: remove all occurrences of a character in a list python 
Python :: discord.py send image 
Python :: How to find least common multiple of two numbers in Python 
Python :: how to make a discord bot dm someone python 
Python :: get current week python 
Python :: install gtts 
Python :: len table python 
Python :: tkinter center frame 
Python :: how to change voice of pyttsx3 
Python :: string array to float array python 
Python :: send email python 
Python :: python generate rsa key pair 
Python :: get median of column pandas 
Python :: how to stop the program in python 
Python :: linux uninstall python 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =