Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 12 characters in body
Source Link
Nazim Kerimbekov
  • 4.8k
  • 10
  • 37
  • 58

I want Django to display an image when accessed. For example, I want to display only the image as a response, such as when accessing the image below. And I want to do the urlURL as a normal URL without using .jpeg or .png.

sample Sample:

sample

But now, no matter what method you try, it doesn't work.

from django.http import HttpResponse import os from django.core.files import File import codecs def image(request): file_name = os.path.join( os.path.dirname(__file__), '12.jpg' ) try: with open(file_name, "rb") as f: a = f.read() return HttpResponse(a, content_type="image/jpeg") except IOError: red = Image.new('RGBA', (1, 1), (255, 0, 0, 0)) response = HttpResponse(content_type="image/jpeg") red.save(response, "JPEG") return response urlpatterns = [ url('image', image) ] 

With the above code, the following error occurs.

'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 

I want Django to display an image when accessed. For example, I want to display only the image as a response, such as when accessing the image below. And I want to do the url as a normal URL without using .jpeg or .png.

sample

But now, no matter what method you try, it doesn't work.

from django.http import HttpResponse import os from django.core.files import File import codecs def image(request): file_name = os.path.join( os.path.dirname(__file__), '12.jpg' ) try: with open(file_name, "rb") as f: a = f.read() return HttpResponse(a, content_type="image/jpeg") except IOError: red = Image.new('RGBA', (1, 1), (255, 0, 0, 0)) response = HttpResponse(content_type="image/jpeg") red.save(response, "JPEG") return response urlpatterns = [ url('image', image) ] 

With the above code, the following error occurs.

'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 

I want Django to display an image when accessed. For example, I want to display only the image as a response, such as when accessing the image below. And I want to do the URL as a normal URL without using .jpeg or .png.

Sample:

sample

But now, no matter what method you try, it doesn't work.

from django.http import HttpResponse import os from django.core.files import File import codecs def image(request): file_name = os.path.join( os.path.dirname(__file__), '12.jpg' ) try: with open(file_name, "rb") as f: a = f.read() return HttpResponse(a, content_type="image/jpeg") except IOError: red = Image.new('RGBA', (1, 1), (255, 0, 0, 0)) response = HttpResponse(content_type="image/jpeg") red.save(response, "JPEG") return response urlpatterns = [ url('image', image) ] 

With the above code, the following error occurs.

'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 
Source Link
django
  • 139
  • 1
  • 12

I want to respond images with Django

I want Django to display an image when accessed. For example, I want to display only the image as a response, such as when accessing the image below. And I want to do the url as a normal URL without using .jpeg or .png.

sample

But now, no matter what method you try, it doesn't work.

from django.http import HttpResponse import os from django.core.files import File import codecs def image(request): file_name = os.path.join( os.path.dirname(__file__), '12.jpg' ) try: with open(file_name, "rb") as f: a = f.read() return HttpResponse(a, content_type="image/jpeg") except IOError: red = Image.new('RGBA', (1, 1), (255, 0, 0, 0)) response = HttpResponse(content_type="image/jpeg") red.save(response, "JPEG") return response urlpatterns = [ url('image', image) ] 

With the above code, the following error occurs.

'utf-8' codec can't decode byte 0xff in position 0: invalid start byte