8.1. Creating the homepage banners

First, we'll create a Banner class to manage the banners on the homepage. Each banner will have several properties, including an image, an output link (which determines where users are redirected upon clicking), and an active state to indicate whether the banner's link is currently enabled or not.

models.py
class Banner(models.Model) :
    image = models.ImageField(null=True, blank=True)
    link_output = models.CharField(max_length=400, null=True, blank=True) #? where the user will be sent after clicking the banner
    active = models.BooleanField(default=False) #? whether the link is currently active or not

    def __str__(self):
        return f"{self.link_output} - Active: {self.active}"

Additionally, we implemented a __str__ method to enhance object representation, providing a clear overview of where each banner redirects users and whether it is currently active.

Now we will migrate the changes:

cmd prompt
python manage.py makemigrations
cmd prompt
python manage.py migrate

Lastly, we will register the new Banner class inside our admin.pyfile so it can appear on the admin page:

admin.py
admin.site.register([Categoric, Client, Type, Product, ItemStock, Order, OrderedItem, Adres, Banner])
We can now add and edit banners inside our admin page

Last updated