teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

修正

2020/01/13 09:52

投稿

django
django

スコア19

title CHANGED
File without changes
body CHANGED
@@ -20,9 +20,15 @@
20
20
  os.path.dirname(__file__),
21
21
  '12.jpg'
22
22
  )
23
+ try:
24
+ with open(file_name, "rb") as f:
23
- if not file_name:
25
+ a = f.read()
26
+ return HttpResponse(a, content_type="image/jpeg")
27
+ except IOError:
28
+ red = Image.new('RGBA', (1, 1), (255, 0, 0, 0))
29
+ response = HttpResponse(content_type="image/jpeg")
30
+ red.save(response, "JPEG")
24
- return HttpResponse()
31
+ return response
25
- return HttpResponse(File(codecs.open(file_name, 'r', 'utf-8', 'ignore')), content_type="image/jpeg")
26
32
 
27
33
 
28
34
 
@@ -31,4 +37,10 @@
31
37
  url('image', image)
32
38
  ]
33
39
 
40
+ ```
41
+
42
+
43
+ 上記のコードだと下のエラーが発生します。
44
+ ```
45
+ 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
34
46
  ```