Skip to main content

Wagtail Column Blocks

Project description

A generic, reusable column block for Wagtail's StreamField.

Allows developers to create column layouts with a number of different layout ratios that are supported in the admin in a visually appealing way.

Admin interface showing column blocks in a streamfield

A basic frontend template is included, but no frontend CSS. Due to the variations of responsive layouts and frontend layout frameworks, frontend templates should be overridden by the developer.

Installation

Once you have set up Wagtail, you should just need to do the following:

  1. pip install wagtailcolumnblocks
  2. Edit INSTALLED_APPS in your settings.py to include 'wagtailcolumnblocks'.

Usage Example

Assuming you have set up Wagtail according to these instructions (and those above), you can see wagtailcolumnblocks in action as follows.

Edit home/models.py to look like this. HomePage is from the original templated code, and we've added SidebarPage and the *Blocks classes:

from django.db import models

from wagtail.core.models import Page
from wagtail.core import blocks
from wagtail.core import fields
from wagtail.embeds.blocks import EmbedBlock
from wagtail.images.blocks import ImageChooserBlock
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel

from wagtailcolumnblocks.blocks import ColumnsBlock


class MyContentBlocks(blocks.StreamBlock):
    """
    The blocks you want to allow within each MyColumnBlocks column.
    """

    image = ImageChooserBlock()
    text = blocks.CharBlock()


class MyColumnBlocks(blocks.StreamBlock):
    """
    All the root level blocks you can use
    """
    column_2_1 = ColumnsBlock(
        # Blocks you want to allow within each column
        MyContentBlocks(),
        # Two columns in admin, first twice as wide as the second
        ratios=(2, 1),
        # Used for grouping related fields in the streamfield field picker
        group="Columns",
        # 12 column frontend grid (this is the default, so can be omitted)
        grid_width=12,
        # Override the frontend template
        template='home/blocks/two_column_block.html',
    )


class SidebarPage(Page):
    content = fields.StreamField(MyColumnBlocks)

    content_panels = [
        FieldPanel('title'),
        StreamFieldPanel('content')
    ]

class HomePage(Page):
    pass

Edit home/templates/home/sidebar_page.html to look like this:

{% extends "base.html" %}
{% load static %}
{% load wagtailcore_tags %}

{% block content %}
<div class="row">
    {{page.content}}
</div>
{% endblock content %}

Edit home/templates/home/blocks/two_column_block.html to look like this:

{% load wagtailcore_tags %}

<div class="row">
    {% for column, width in columns %}
        <div class="col-xs-12 col-md-6">
            Column width = {{ width }}
            {% include_block column %}
        </div>
    {% endfor %}
</div>

Run python manage.py makemigrations && python manage.py migrate to add our SidebarPage migration and execute it.

In the Wagtail admin you should now be able to create a Sidebar page which shows up in a two column layout. When you view or preview the page you should see the templated content.

License

Licensed under the BSD 3-clause "New" or "Revised" License.

(c) 2019, Squareweave. All rights reserved.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtailcolumnblocks-2.2.0.tar.gz (421.2 kB view details)

Uploaded Source

File details

Details for the file wagtailcolumnblocks-2.2.0.tar.gz.

File metadata

  • Download URL: wagtailcolumnblocks-2.2.0.tar.gz
  • Upload date:
  • Size: 421.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.7

File hashes

Hashes for wagtailcolumnblocks-2.2.0.tar.gz
Algorithm Hash digest
SHA256 c6a9c8122315b46822af58e5829e0b2ad671ecfc5ad1108cf3427aecfcca9418
MD5 16146f1978973144b7f370720562e7d3
BLAKE2b-256 77b878397e04f9ca6e41367cec3911355bce30f6e356f7ce09299cf22ff8f791

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page