Multiple Images?

853 views
Skip to first unread message

RedspartHD

unread,
Jan 6, 2016, 2:51:41 PM1/6/16
to Wagtail support
Hey Wagtail support/forum,

I am trying to figure out how to create a page that can add and remove multiple images ( dynamically ) in the Wagtail /cms/. For example, one page will have a list of people including their images, titles, and sub-titles ( if any ) -- do I have to add each person manually to the model ( main_image# = ...)? Or is there some type of field that I can use to get this done? The end result would be that: end-user logs in adds or removes a new image/title/sub and it is affected on the page. I have done this in Django using django-inlineedit, but I want to switch to a CMS. 

Best,
Red. 

Brett Grace

unread,
Jan 6, 2016, 6:43:41 PM1/6/16
to Wagtail support
Wagtail doesn't support inline editing, it only offers content editing through its backend area. I don't understand what you mean by "do I have to add each person manually to the model?"
Message has been deleted

RedspartHD

unread,
Jan 6, 2016, 6:55:34 PM1/6/16
to Wagtail support
I understand it does not offer inline-editing, I was just saying that I can do this inline--but prefer not to. What I am trying to do is display 50 or so images with names under it, but have the ability to delete/add new ones via the backend of Wagtail. From testing I have to add a new image to the model for each which really hinders what I am trying to do due to the unkown of additions/removals. Is there any way to do this? I am stumbled.

Thanks for the help!

Brett Grace

unread,
Jan 6, 2016, 9:22:50 PM1/6/16
to Wagtail support
Oh, I see. Yes, you can edit collections of Images (or any other model).

You can either create a standard Django model with a ParentalKey to the page, or you can create a type of StreamBlock. Both techniques are shown in the demo: https://github.com/torchbox/wagtaildemo/blob/master/demo/models.py

ParentalKey fields are similar to ForeignKey fields, with support added for some Wagtail-specific features (preview and revisions). StreamBlocks can be used to store structured data without creating explicit models. Either can store a reference to an Image object.

For examples of ParentalKey and StreamBlock:

RedspartHD

unread,
Jan 7, 2016, 3:31:02 PM1/7/16
to Wagtail support
Thank you so much! I decided to go with Streamblocks. It was all good and dandy untill I ran a makemigrations. Now I am getting something like this:

You are trying to add a non-nullable field 'carousel' to homepage without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py


Enter code here...from __future__ import unicode_literals

from wagtail.wagtailcore import blocks
from wagtail.wagtailcore.blocks import *
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField, StreamField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.wagtailimages.blocks import ImageChooserBlock
from django import forms

class PageN(FieldBlock):
field = forms.ChoiceField(choices=(
('/about/', "About Us"),
('/recruitment/', "Recruitment"),
('/brothers', "Brothers"),
('/alumni/', "Alumni"),
('/contact/', "Contact Us")
))

class PersonBlock(blocks.StreamBlock):
carousel = blocks.StructBlock([
(('image'), ImageChooserBlock()),
(('title'), blocks.CharBlock(max_length=120, blank=True, null=True, default='Title')),
(('subtext'), blocks.CharBlock(max_length=120, blank=True, null=True, default='none')),
(('link'), PageN()),
])


class HomePage(Page):
body = RichTextField(blank=True)
carousel = StreamField(PersonBlock())


HomePage.content_panels = [
FieldPanel('title'),
StreamFieldPanel('carousel'),
FieldPanel('body'),
]



With this code. I have tried playing around with blank, null, and default, but can not figure it out to save my life. Any ideas?

Matthew Westcott

unread,
Jan 7, 2016, 8:33:10 PM1/7/16
to wag...@googlegroups.com
Select 1, and enter '' (two quotes). This is because you're providing the default value in the form of a Python expression (e.g. if you were adding a numeric field, you'd usually use 0 here), and StreamFields are string-based at the database level

Alternatively, you can avoid the question by setting null=True in the model definition:
StreamField(PersonBlock(), null=True)
StreamField doesn't treat null vs non-null fields any differently, so this shouldn't make any difference to the behaviour.

Cheers,
- Matt
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
> To post to this group, send email to wag...@googlegroups.com.
> Visit this group at https://groups.google.com/group/wagtail.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/ac4fe9a1-d68e-4cb3-be92-cd88b59526d8%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages