Joe Howard
Joe Howard

Reputation: 307

Use an image in Wagtail's Base.html template

I'm looking to add a logo to my base.html by pulling from the page model... But I don't have access to the base.html page model through wagtail, and so I don't know where to create a ImageChooserPanel to specify the logo for my navbar (which should be applied to all pages).

Upvotes: 2

Views: 918

Answers (1)

Dan Swain
Dan Swain

Reputation: 3098

In your models.py file add a setting:

from django.db import models
from wagtail.contrib.settings.models import BaseSetting, register_setting
from wagtail.images.models import Image
from wagtail.images.edit_handlers import ImageChooserPanel

@register_setting
class SiteSettings(BaseSetting):
    logo = models.OneToOneField(Image, null=True, blank=True, on_delete=models.SET_NULL, related_name='+', verbose_name='Business logo')
    panels = [
        ImageChooserPanel('logo'),
    ]

In your admin Settings menu you will now see Site Settings. Click on that to choose your logo.

In your template at the top:

{% load wagtailimages_tags %}

And then render with:

{% image settings.app_label.SiteSettings.logo width-300 %}

where app_label is the app/folder where you have your models.py file.

Reference: https://docs.wagtail.io/en/latest/reference/contrib/settings.html#using-in-django-templates

Upvotes: 5

Related Questions