Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
10224 Lượt xem

Hi,

I want to display image in my custom module from url. I have an api and I  fetched the image url such as thumbnail from it. But while doing like given below code , its not worked.  

Thanks in advance

class img(models.Model):
_name = 'img.books'

photo = fields.Binary(string='Image')

def search_button(self):
url = "https://www.googleapis.com/books/v1/volumes?q=" + isbn_no;
response = requests.get(url)
json_data = response.json()
loaded_json = json.dumps(json_data)
book = json_data['items'][0]
image_url = book["volumeInfo"]['imageLinks']['smallThumbnail'] // here contains image url such as thumbnail
            
img_obj =Image.open(BytesIO(image_url.content))
buffer = BytesIO()
img_obj.save(buffer, format="JPEG")
img = base64.b64encode(buffer.getvalue().strip())
self.photo = img


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

Try to rewrite your code like below

import base64
import requests

class img(models.Model):
_name = 'img.books'

photo = fields.Binary(string='Image')

def search_button(self):
url = "https://www.googleapis.com/books/v1/volumes?q=" + isbn_no;
response = requests.get(url)
json_data = response.json()
loaded_json = json.dumps(json_data)
book = json_data['items'][0]
image_url = book["volumeInfo"]['imageLinks']['smallThumbnail'] // here contains image url such as thumbnail
img = base64.b64encode(requests.get(image_url).content)
            self.photo = img

Regards

Ảnh đại diện
Huỷ bỏ
Tác giả

Thank you Cybrosys Techno Solutions.....

Its working

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 7 22
12067
0
thg 2 21
1911
5
thg 5 25
33911
0
thg 11 23
1920
0
thg 10 23
1542