8.2. Dynamic banner links

Next, we'll add functionality to support clickable banners. To achieve this, we'll include the banners in the homepage's URL routing by modifying the views.py file.

def homepage(request): #? the first parameter always has to be a request
    banners = Banner.objects.all()
    context = {"banners" : banners}
    return render(request, 'homepage.html', context) #? returns the homepage

In the homepage.html file, we'll display the banners by iterating over the list of previously created banners. The output URL for each banner will be derived from the base homepage URL, appended with the corresponding category name, ensuring accurate redirection based on the banner's category.

homepage.html
{% for banner in banners %}

<a href="/{{ banner.link_output }}"> <!-- joining the homepage url (using '/') to the product url from the banner -->
    <img src = "{{ banner.image.url}}">
</a>

{% endfor %}

When the page is launched, all banners are displayed with clickable functionality, allowing users to interact with them. However, please note that the redirected page won't work, as the target pages for these links have not yet been implemented.

Last updated