简体   繁体   中英

Displaying image URL Django

who knows what the problem is and how to fix it, I will try to explain. I have 2 projects, 2 databases: a warehouse and a store, when a new product arrives at the warehouse, the selery performs the task and checks how much product needs to be added to the Store's database, or if it is a new product, then it adds it. problem with pictures.

Model Warehouse:

class Book(models.Model):
    author = models.ForeignKey('Author', on_delete=models.CASCADE)
    genre = models.ForeignKey(Genre,
                              related_name='books',
                              on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    language = models.CharField("language", max_length=20)
    pages = models.IntegerField()
    image = models.ImageField(upload_to='products/%Y/%m/%d')
    slug = models.SlugField(max_length=255)
    price = models.DecimalField(max_digits=10, decimal_places=2)

    isbn = models.CharField('ISBN', max_length=13,
                            unique=True)
    created = models.DateTimeField(auto_now_add=True)
    available = models.BooleanField(default=True)
    quantity = models.IntegerField()

here everything is fine, it saves pictures in media / products

Model Store:

class Book(models.Model):
    author = models.ForeignKey('Author', on_delete=models.CASCADE)
    genre = models.ForeignKey(Genre,
                              related_name='books',
                              on_delete=models.CASCADE)

    title = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    language = models.CharField("language", max_length=20)
    pages = models.IntegerField()
    image = models.URLField(validators=[URLValidator()])
    slug = models.SlugField(max_length=255)
    price = models.DecimalField(max_digits=10, decimal_places=2)

    isbn = models.CharField('ISBN', max_length=13,
                            unique=True)
    created = models.DateTimeField(auto_now_add=True)
    available = models.BooleanField(default=True)
    quantity = models.IntegerField()

What is the correct name for a field in the store image?

link to task celery: https://dpaste.com/G6HJBSJAY

screen from API and Image url

after celery task how I get in the Store screen from DB in the store

It's a image field in your store project. here is the way you can render it on the html :-

<img src="{{book.image}}" alt="Book image" width="500" height="600">

Let consider your DB store below data into the image field in store project.

complete URL :-

https://1.bp.blogspot.com/-63yzpaIPb8o/YSz6iG-BoRI/AAAAAAAAMr4/6iQ-opW8qgoKi4Pc69iMakii4NdVUTQ6ACLcBGAsYHQ/s2048/Jain-Panchang-Calendar-2021-September.jpg

The image tag will be :-

<img src="https://1.bp.blogspot.com/-63yzpaIPb8o/YSz6iG-BoRI/AAAAAAAAMr4/6iQ-opW8qgoKi4Pc69iMakii4NdVUTQ6ACLcBGAsYHQ/s2048/Jain-Panchang-Calendar-2021-September.jpg" alt="Book image" width="500" height="600">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM