Created
February 6, 2019 19:26
-
-
Save KalobTaulien/a72f6c4757be4283bbd7079756062812 to your computer and use it in GitHub Desktop.
This is the code from the learnwagtail.com lesson where we add an Orderable to the Home Page to create a Bootstrap 4 Carousel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# home/models.py | |
from django.db import models | |
from modelcluster.fields import ParentalKey | |
from wagtail.admin.edit_handlers import ( | |
FieldPanel, | |
MultiFieldPanel, | |
InlinePanel, | |
StreamFieldPanel, | |
PageChooserPanel, | |
) | |
from wagtail.core.models import Page, Orderable | |
from wagtail.core.fields import RichTextField, StreamField | |
from wagtail.images.edit_handlers import ImageChooserPanel | |
from streams import blocks | |
class HomePageCarouselImages(Orderable): | |
"""Between 1 and 5 images for the home page carousel.""" | |
page = ParentalKey("home.HomePage", related_name="carousel_images") | |
carousel_image = models.ForeignKey( | |
"wagtailimages.Image", | |
null=True, | |
blank=False, | |
on_delete=models.SET_NULL, | |
related_name="+", | |
) | |
panels = [ImageChooserPanel("carousel_image")] | |
class HomePage(Page): | |
"""Home page model.""" | |
template = "home/home_page.html" | |
max_count = 1 | |
banner_title = models.CharField(max_length=100, blank=False, null=True) | |
banner_subtitle = RichTextField(features=["bold", "italic"]) | |
banner_image = models.ForeignKey( | |
"wagtailimages.Image", | |
null=True, | |
blank=False, | |
on_delete=models.SET_NULL, | |
related_name="+", | |
) | |
banner_cta = models.ForeignKey( | |
"wagtailcore.Page", | |
null=True, | |
blank=True, | |
on_delete=models.SET_NULL, | |
related_name="+", | |
) | |
content = StreamField([("cta", blocks.CTABlock())], null=True, blank=True) | |
content_panels = Page.content_panels + [ | |
MultiFieldPanel( | |
[ | |
FieldPanel("banner_title"), | |
FieldPanel("banner_subtitle"), | |
ImageChooserPanel("banner_image"), | |
PageChooserPanel("banner_cta"), | |
], | |
heading="Banner Options", | |
), | |
MultiFieldPanel( | |
[InlinePanel("carousel_images", max_num=5, min_num=1, label="Image")], | |
heading="Carousel Images", | |
), | |
StreamFieldPanel("content"), | |
] | |
class Meta: | |
verbose_name = "Home Page" | |
verbose_name_plural = "Home Pages" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# templates/home/home_page.html #} | |
{% extends "base.html" %} | |
{% load wagtailcore_tags wagtailimages_tags %} | |
{% block content %} | |
{% image self.banner_image width-3560 as img %} | |
<div class="jumbotron" style="background-image: url('{{ img.url }}'); min-height: 400px; height: 40vh; background-size: cover; background-position: center top; display: flex; flex-direction: column; justify-content: center;"> | |
<h1 class="display-4">{{ self.banner_title }}</h1> | |
<div class="lead">{{ self.banner_subtitle|richtext }}</div> | |
{% if self.banner_cta %} | |
<a class="btn btn-primary btn-lg" href="#" role="button">@todo</a> | |
{% endif %} | |
</div> | |
{# Example of an Orderable from home/models.py #} | |
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel"> | |
<div class="carousel-inner"> | |
{% for loop_cycle in self.carousel_images.all %} | |
{% image loop_cycle.carousel_image fill-900x400 as img %} | |
<div class="carousel-item{% if forloop.counter == 1 %} active{% endif %}"> | |
<img src="{{ img.url }}" class="d-block w-100" alt="{{ img.alt }}"> | |
</div> | |
{% endfor %} | |
</div> | |
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev"> | |
<span class="carousel-control-prev-icon" aria-hidden="true"></span> | |
<span class="sr-only">Previous</span> | |
</a> | |
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next"> | |
<span class="carousel-control-next-icon" aria-hidden="true"></span> | |
<span class="sr-only">Next</span> | |
</a> | |
</div> | |
{% for block in page.content %} | |
{% include_block block %} | |
{% endfor %} | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The full git commit can be found here: CodingForEverybody/learn-wagtail@e280dfa
The full lesson on learnwagtail.com can be found here: https://learnwagtail.com/tutorials/how-use-orderables-wagtail-cms/