This is part two of our blog series about providing initial data in Django web projects. If you did not read part one, you should read it first.
This blog post will explain in detail how you can hook various project data setup management commands into the
total_reset command which we presented in part one. Continue reading to learn how we approached this task for codista.com - a Django / Wagtail based website.
Setup the Wagtail Page Tree
While starting to work with Wagtail a few years ago, we had the problem that all team members had to manually create the page tree. This website for example has two
HomePages (one for german and one for english), two
ContactPages , two
ProjectIndex pages, multiple
ProjectDetailPage pages and so on. This task is especially tedious if you are the only developer working on the project.
So what did we do about it? We automated the task of setting up the website page tree. This was probably one of the best decisions we made since it saved our entire team hours of manual work and ensures that each team member always have a working copy of the entire website on the tip of their fingers… just type
./manage.py total_reset, wait a few minutes and voilá .. a fresh website is up and running with real data.
When providing your team members with initial content, it makes their life much easier and supports them in being more productive.
Here's an overview of how the
setup_page_tree management command is structured, which is responsible for setting up the page tree. You can find the final snipped in our GitHub repository.
class Command(BaseCommand): """ this command is used to create the initial wagtail cms page tree """ requires_system_checks = False def _setup(self): self._setup_language_redirection() self._setup_home() self._setup_team_member_index() self._setup_team_member_pages() # finally, create the menus self._create_main_menu() self._create_flat_menus() def _setup_language_redirection(self): """First things first, tear down the dummy root page, and setup our language_redirection page """ ... def _setup_home(self): """Creates the language specific home pages.""" ... def _setup_team_member_index(self): """Creates the language specific team member pages.""" ... def _setup_home(self): """Creates the language specific home pages.""" ... def handle(self, raise_error=False, *args, **options): """entry point""" ...
setup_page_tree management command is just one example of setting up initial project data for a Wagtail based CMS Django website. For more data heavy use cases you could use FactoryBoy or loading fixtures data as defined in the Django docs.
So, you finally reached the end. This blog post ended up to be a bit longer than expected, but I hope you enjoyed reading it and learned a thing or two.
Takeaways from this series
Automating the process of creating initial data takes some work, but from our point of view this work is worth it, because:
- It will save you and your team tons of time
- It standardizes the setup and makes the process less error prone
- It makes your team happy and helps to share a common state across the entire team.