x

Add Wagtail to Django

24 Apr 2021 / OscFr
Web logo

If you have a website with Django you can easily add a Wagtail app to it.
Just follow this simple steps.

Installation

Download and go to app

git clone https://github.com/oscfr657/Birdapp657.git
cd Birdapp657

Pip requirements

pip install -r requirements.txt

Django settings

To your settings file,
add to the INSTALLED_APPS

'django.contrib.sites',  # custom
    'django.contrib.sitemaps',  # custom

    # wagtail
    'wagtail.contrib.forms',
    'wagtail.contrib.redirects',
    'wagtail.contrib.settings',  # custom
    'wagtail.contrib.search_promotions',  # custom
    'wagtail.embeds',
    'wagtail.sites',
    'wagtail.users',
    'wagtail.snippets',
    'wagtail.documents',
    'wagtail.images',
    'wagtail.search',
    'wagtail.admin',
    'wagtail.core',
    'modelcluster',
    'taggit',

    # custom
    'birdapp657',
    # birdapp657 media file block
    'wagtailmedia',
    # birdapp657 default search backend
    'wagtail.contrib.postgres_search',

add to the MIDDLEWARE settings

'django.contrib.sites.middleware.CurrentSiteMiddleware',
    'wagtail.contrib.redirects.middleware.RedirectMiddleware',

add to the TEMPLATES settings

TEMPLATES = [
        {
            'OPTIONS': {
                'context_processors': [
                    'wagtail.contrib.settings.context_processors.settings',  # Extra
            },
        },
    ]

I recommend

set the

WAGTAILIMAGES_FORMAT_

CONVERSIONS

setting:

WAGTAILIMAGES_FORMAT_CONVERSIONS = {
    'bmp': 'webp',
    'jpeg': 'webp',
    'jpg': 'webp',
    'JPG': 'webp',
    'webp': 'webp',
    'png': 'webp',
}

Database configuration

python manage.py migrate

Search Index setup

python manage.py update_index

Django url

To the django projects' url.py add

from django.urls import path, re_path
from django.conf.urls import include

# Wagtail
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.core import urls as wagtail_urls

from wagtail.contrib.sitemaps.views import sitemap

and

urlpatterns = [
    re_path('sitemap.xml', sitemap),
    #  Wagtail
    re_path(r'^birdapp/', include(wagtailadmin_urls)),
    re_path(r'^documents/', include(wagtaildocs_urls)),
    re_path(r'', include(wagtail_urls)),
  ]

optionally

handler403 = 'birdapp657.views.bird_page_403'
handler404 = 'birdapp657.views.bird_page_404'
handler500 = 'birdapp657.views.bird_page_500'

Collectstatic

python manage.py collectstatic
Twitter logo black LinkedIn logo black Facebook “f” logo black Reddit logo black Email icon black Print logo black