Creating Page Models and images programatically

3,459 views
Skip to first unread message

Christian Peters

unread,
Sep 11, 2015, 4:12:23 AM9/11/15
to Wagtail support
We move a very old, very big, 90ties, PHPish peace of horror to wagtail.

I have written a script that somehow organizes the content from the old database. Next thing is the import into wagtail.

I have the images available (StringIO, Binary, doesn't matter) and page content (text or basic html) and title and the structure of page tree.

What I want to do: 

- create a root page 
- inject the original text and add the images (it does not have to look great as we have editors getting over it afterwards)
- add the child page to that root page with text and images
- recursively doing so until everything is smooth and shiny.

So what I need to understand is the best approach to create a Page() and an Image() in python - are there docs or anything to help? I would dive into the source if not :)


Christian Peters

unread,
Sep 11, 2015, 5:31:28 AM9/11/15
to Wagtail support
Okay, pages was rather easy

parent_page = Page.objects.get(id=site_root_id).specific
page = HomePage(
search_description='',
seo_title='foo',
show_in_menus=False,
slug='foobar',
title='Foo Bar'
)

page.body.stream_data = [
('heading', 'This is a heading'),
('paragraph', RichText("<p>I'm a heading</p>"))
]

parent_page.add_child(instance=page)
revision = page.save_revision(
user=self.import_user,
submitted_for_moderation=False,
)
revision.publish()
page.save()

Karl Hobley

unread,
Sep 11, 2015, 7:05:49 AM9/11/15
to wag...@googlegroups.com
Hi Christian,

When you first create a page, you must use the add_child method of the page that you want to create it under.

For example, instead of calling page.save():

from wagtail.wagtailcore.models import Page

root_page = Page.get_root_nodes()[0]
root_page.add_child(instance=page)


This saves the page to the database, adding it underneath the "root" page.


For Images, you just need to create an instance of wagtailimages.models.Image (or your custom image model if you have one). You also need to convert the StringIO (Python 2)/BytesIO (Python 3) you have to an ImageFile object.

For example:

from django.core.files.images import ImageFile
from wagtail.wagtailimages.models import Image

image = Image(
    title="Image title",

    # image_file is your StringIO/BytesIO object
    file=ImageFile(image_file, name="image-filename.jpg"),
)
image.save()

Django will automatically place your image file into the media folder.


Hope that helps,

Karl


--
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 http://groups.google.com/group/wagtail.
To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/78cb3d42-485b-405d-b8b5-95831f635e46%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Christian Peters

unread,
Sep 15, 2015, 4:52:19 AM9/15/15
to wag...@googlegroups.com
Works like a charm!

--
You received this message because you are subscribed to a topic in the Google Groups "Wagtail support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wagtail/Get0wrrJ-3c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wagtail+u...@googlegroups.com.

Diego Martinez

unread,
Aug 4, 2016, 8:47:16 AM8/4/16
to Wagtail support
Can you copy the entire file? don't work for me. :( 

I want to create all pages of my app programmatically with default values, not inside of wagtail admin. i have the root page and rest of the pages will be child of root page, but only i can create it whit the admin panels, not programmatically.

Regards, 
Diego

Tobias McNulty

unread,
Aug 4, 2016, 5:51:11 PM8/4/16
to wag...@googlegroups.com
What's the error you're seeing?

Tobias

--
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.

For more options, visit https://groups.google.com/d/optout.



--

Tobias McNulty
Chief Executive Officer

tob...@caktusgroup.com
www.caktusgroup.com

Brett Grace

unread,
Aug 5, 2016, 9:38:15 PM8/5/16
to Wagtail support
Or, even better, post your code so we can work through it together.

Erin Mullaney

unread,
Aug 8, 2016, 8:51:26 AM8/8/16
to Wagtail support
Hi there! I wrote up a blog post about adding Wagtail pages via the back-end and it might help you.


Best,
Erin

Isaac Csandl

unread,
Oct 17, 2016, 4:27:23 PM10/17/16
to Wagtail support
Loosely related: a gist for grabbing a remote image from another web server and saving it directly into Wagtail:


I used it as part of importing events from a Facebook Page to my client's site, with the image included.

HTH

--Isaac

Oluwaseun Omotosho

unread,
Jul 19, 2018, 1:48:27 AM7/19/18
to Wagtail support
Hi Erin,

Many thanks to you for that post it really helped.

But i think it no longer works for wagtail2.1.1 which is the most recent release. .. in fact i just discovered today

Cheers
Reply all
Reply to author
Forward
0 new messages