[Answered ]-How django handle binary post data?

0👍

Looks like the 'user_logo' is a JPEG file encoded in base16/hex (except the prefix '<')

>>> 'ffd8ffe000104a4649460001010100'.decode('hex')
'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00'

You could use ContentFile to store this sort of data

obj.image.save(filename, ContentFile(the_decoded_content))

Also, better to ask the dude implementing client-side whether he could post in multipart/form-data, if so the user_logo could be easily accessed through request.FILES…and less request body

👤okm

2👍

models.py

class Image(models.Model):
    image = models.ImageField(upload_to='image_uploads')
    created = models.DateTimeField(auto_now_add=True)

forms.py

class ImageForm(ModelForm):
    class Meta:
        model = Commercial

views.py

def add_image(request):
    if request.method == 'POST':
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
return direct_to_template(request, 'page.html', {
    'form': ImageForm()
})

page.html

<form method="post" enctype="multipart/form-data">
{% csrf_token %}
    {{form.image}}
</form>

0👍

As a rule you don’t want to store images in databases. Django’s FileField and ImageField load the data off the disk.

Edit: just noticed the title had to do with post data. Check out the article on file uploads

Leave a comment