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>
- [Answered ]-Is there a typical mod_wsgi project directory?
- [Answered ]-Dynamically import url in Django gives 'str' object is not callable
- [Answered ]-Django cache, weather site auto-refresh cache every 5 minutes
- [Answered ]-Django with AWS – Correct way to syncdb and run scheema migrations using South
- [Answered ]-How to hydrate ToOneField and ToManyField in tastypie
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
- [Answered ]-Nested Json with multipart/form-data in AFNetworking 2.x
- [Answered ]-GeoDjango point geom field
Source:stackexchange.com