0

I have two Models in my application that are related to each other (Item and ItemGallery). In my Template, I want the user to have the option of Edit Gallery or Add Gallery. Once the User has entered the image gallery belong to an item before the Interface should change to Edit Gallery. If not, the Interface should be Add Gallery. In a short world, I should be able to know if an item has or have a gallery image or not. If it has gallery then, edit Gallery if not Add Gallery

 {% if  mylists.itemgallery.all >= 1 %} 
       <a href="{% url 'add_gallery' mylists.id %}">
                                                        <i class="fa fa-pencil"></i>Edit Gallery
                                                    </a>
                                                {% else %}
   <a href="{% url 'add_gallery' mylists.id %}">
          <i class="fa fa-pencil"></i>Add Gallery
         </a>

    {% endif %}

models.py

class Item(models.Model):

STATUS = (
    ('Used', "Used"),
    ('New', "New"),
    ('Fairly Used', 'Fairly Used'),
    ('Tokunbo', 'Tokunbo'),
)

ITEM_TYPE = (
    ('Demand', "Demand"),
    ('Offer', "Offer"),
)

SALES_TYPE = (
    ('By Dealer', "By Dealer"),
    ('By Owner', "By Owner"),
)

CAR_MAKE = (
    ('Toyota', "Toyota"),
    ('Nissan', "Nissan"),
    ('Audi', 'Audi'),
    ('Honda', 'Honda'),
    ('Volkswagen', 'Volkswagen'),
    ('Mercedes Benz', 'Mercedes Benz'),
    ('Land Rover', 'Land Rover'),
    ('BMW', 'BMW'),
)


QUALIFICATION = (
    ('PhD', "PhD"),
    ('BSc', "BSc"),
    ('HND', "HND"),
    ("O'Level", "O'level"),
)














name = models.CharField('Add Title', max_length=100)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
description = models.TextField('Full Detail of the Add')
role = models.TextField('Job Role Detail')
image = models.ImageField(upload_to='item') 
status = models.CharField(max_length=50, choices=STATUS, null=True, blank=True)
item_type = models.CharField('Type of Item', max_length=20, choices=ITEM_TYPE, null=True, blank=True)
compensation =  models.DecimalField("Conpensation", default='000.00', max_digits=10, decimal_places=2, null=True, blank=True)
price = models.DecimalField("Asking Price", default='000.00', max_digits=10, decimal_places=2, null=True, blank=True)

created_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)


def get_absolute_url(self):
    return reverse('item_details', kwargs={'pk': self.pk})


def __str__(self):
    return f'{self.name} - {self.user.email}'



class ItemGallery(models.Model):    
    item = models.ForeignKey(Item, related_name='itemgallery', on_delete=models.CASCADE)
    image = models.ImageField(upload_to='gallery')
    created_at = models.DateTimeField(auto_now_add=True)
    update_at = models.DateTimeField(auto_now=True)

def __str__(self):
    return f'{self.item.name} - {self.image}'

enter image description here

0 Answers0