Display dictionary data or images in the Django template

Asked 1 years ago, Updated 1 years ago, 106 views

Hi, everyone. I would like to ask you how to display the image data downloaded by scrapy on the long-term template. First of all, the data field downloaded by scrapy is connected to the Django model using a long-range item. The contents of the image field that is down are in a dictionary ({key1 : "value1", key2 : "value2", key3 : "value3"}) Double key2 : "value2" is the image pass stored on the local disk I'm trying to put this value2 into a long-term template, but it doesn't work crying (I'm trying to do image scr, not image pass.)

First, to see if the pass is visible,

image : {{ actress.images['key2'][0] }}

I tried it with and there was an error again.

I searched stackoverflow and found a way to only pick the value from the dictionary I think there's a better way than this, but I can't think of an idea crying And I wonder what other related items of Scrapy (it has two items of name and image (=django model) that are related to the actress list crawling) are there any ways to see them in the template.

You don't have to tell me in detail, so if you know any ideas or simple instructions, please do. ^

^

django scrapy python

2022-09-22 21:29

2 Answers

charfield str dict, rather than type stored dict in a converted to the result is saved I'm afraid. 심지어 [(True, {'checksum': '2b00042f7481c7b056c4b410d28f33cf', 'path': 'full/0a79c461a4062ac383dc4fade7bc09f1384a3910.jpg', 'url': 'http://www.example.com/files/product1.pdf'}), (False, Failure(...))] 이건 dict형도 아니고 리스트네요

When scraping and saving as objects, I think we need a code that cuts the dict type to key-value and appropriately substitutes it into the model field instead of storing the dict type in Charfield immediately.

For example,


data = {'checksum': '2b00042f7481c7b056c4b410d28f33cf', 'path': 'full/0a79c461a4062ac383dc4fade7bc09f1384a3910.jpg', 'url': 'http://www.example.com/files/product1.pdf'}

ActressImage.objects.create(
    value1 = data['checksum'],
    value2 = ...
    )

I think you can write it like this.


2022-09-22 21:29

Register the model as admin.site.register(model name) at admin.py Have you checked the admin page to see if the image address is valid?

I don't know what it means to have the contents of the image field in dictionary. Does the model field mean dictionary in the model? Or does it mean that views.py delivers it in dictionary form?

If the Actress model class has to have the name and image fields, if it were me

# models.py

class Actress(models.Model):
    name = models.CharField(max_length=100)

class ActressImage(models.Model):
    actress = models.ForeignKey(Actress)
    value1 = models.CharField(max_length=100)
    value2 = models.ImageField(upload_to="...")
    value3 = models.CharField(max_length=100)

I'll make two models like this and approach them.


2022-09-22 21:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.