Wagtail CMS for BaseApp
Project description
BaseApp Wagtail - Django
Use this package to load the Wagtail CMS initial Setup. You can also follow its internal code as a reference of how to implement features inside of the Wagtail CMS
Installation
Install the package
Install in your environment:
pip install baseapp-wagtail
Usage
Package Setup
The following steps are already applied in the baseapp-backend-template. If you want to remove the Wagtail package settings from your project, please follow this guide: Remove the Wagtail package from your project.
Import and configure the package settings
Because Wagtail requires a series of settings to work, the package defines the necessary ones. To import and configure these settings, follow the steps below in your project settings file (settings.py or settings/base.py):
-
Import the package settings.
# ... from baseapp_wagtail.settings import * # noqa
-
The
INSTALLED_APPSandMIDDLEWAREentries required for Wagtail to work properly are not configured automatically by the import above. You need to import and add them manually toINSTALLED_APPSandMIDDLEWARE.- Import the settings that need manual configuration:
from baseapp_wagtail.settings import ( WAGTAIL_INSTALLED_APPS, WAGTAIL_INSTALLED_INTERNAL_APPS, WAGTAIL_MIDDLEWARE, )
- Append the
INSTALLED_APPSsettings to theINSTALLED_APPSvariable:
INSTALLED_APPS = [ # ... ] # Wagtail INSTALLED_APPS INSTALLED_APPS += [ # baseapp_wagtail "testproject.base", *WAGTAIL_INSTALLED_INTERNAL_APPS, *WAGTAIL_INSTALLED_APPS, ]
- Append the
WAGTAIL_MIDDLEWAREsetting to theMIDDLEWAREvariable:
MIDDLEWARE = [ # ... ] # Wagtail MIDDLEWARE MIDDLEWARE += WAGTAIL_MIDDLEWARE
-
Add the
WAGTAIL_SITE_NAMEvariable to the settings:WAGTAIL_SITE_NAME = "Baseapp CMS"
-
Ensure you have the following environment variables (ENV vars) registered. For more details, see baseapp_wagtail/settings.py:
URLWAGTAIL_FRONT_URL_PATHWAGTAIL_FRONT_PAGE_PREVIEW_URL_PATH
Register the Wagtail URLs
Inside the urls.py of your project, register the Wagtail URLs as follows:
import baseapp_wagtail.urls as baseapp_wagtail_urls
# ...
urlpatterns = [
# ...
re_path(r"", include(baseapp_wagtail_urls)),
]
Register the Page Model
Wagtail doesn’t add any page models by default, and the package doesn’t either. This is because once a page model is registered, it cannot be unregistered. However, the package provides an abstract page model that’s ready to use; you just need to extend it in the models.py file of any app in your project. Here’s an example:
from baseapp_wagtail.models import BaseStandardPage
class StandardPage(BaseStandardPage):
pass
After that, create the migrations and migrate it.
Customize Wagtail
This package has two main purposes:
- Provide a plug-and-play Wagtail instance with helpful and valuable custom features.
- Create a framework for working with Wagtail.
This section covers the second purpose: a framework to extend and scale Wagtail.
Architecture to Extend Wagtail Features in the Baseapp Templates
The package defines an internal architecture that should ideally be followed by final projects when adding new custom features. This makes customizations portable, allowing valuable features to be easily transferred to the package, enabling its evolution with minimal effort.
The architecture is straightforward and follows these main folders:
api/
base/
├── blocks/
│ ├── basic_blocks/
│ └── custom_blocks/
├── stream_fields/
medias/
- api/: Contains headless API endpoints.
- base/: Stores all page customizations, including available blocks and stream fields.
- medias/: Stores custom images or document configurations.
Everything else follows the standard Wagtail or Django architecture.
Customizing Blocks
The main customizations in Wagtail will be within the block list. Everything in headless Wagtail is block-oriented, so any custom content within pages is likely handled through blocks.
Follow the pattern of existing blocks to create new ones. Here’s a quick guide on where and how to create them:
- Determine if it’s a basic or a custom block. Basic blocks are extensions of Wagtail blocks (e.g.,
RichTextBlock). Custom blocks are entirely new blocks. - In the
base/blocks/basic_blocks/orbase/blocks/custom_blocks/folder, create a folder for your block, naming it in snake_case. - Inside this folder, create an
__init__.pyfile that imports the block from ablock.pyfile. - Define the block within
block.py. - Create a
tests/folder inside this block folder and add unit tests for the block there. Follow the patterns of other blocks to test your new one (any new block has to be added in thePageForTestsmodel inside oftests/model.pyin the root of the project). - After creating the block, add its import within
base/blocks/__init__.py. - Add your block inside the desired
base/stream_field/class. Each page type can use multiple stream fields.
In general, always refer to the package as a guide for customizing Wagtail.
Adding New API Endpoints
To add new API endpoints, review the file baseapp-wagtail/baseapp_wagtail/api/router.py. You'll need to import it into your project and add the new endpoints.
Afterward, override the path("api/v2/", api_router.urls) entry in your project’s urls.py with the updated router. Simply import the modified api_router in urls.py and add the path path("api/v2/", api_router.urls) after where you registered the Wagtail URLs (Register the Wagtail URLs).
Uninstallation
Remove the Wagtail Package from Your Project
To remove the Wagtail package from your project (ideally using the baseapp-frontend-template as a boilerplate), follow these steps:
-
Remove the page model and Wagtail app.
- Revert the page model migration (assuming the page model is under the
wagtail/baseapp)../manage.py migrate base zero
- Delete the
wagtail/folder (rm -rf wagtail/).
- Revert the page model migration (assuming the page model is under the
-
Revert the Wagtail package migrations.
./manage.py wagtail_revert_package_migrationsThis command might be unstable, as it requires updates when new features are added to the package. If the script crashes or if Wagtail tables still exist in the database, proceed to the next steps, then drop and recreate the database schema to prevent the Wagtail migrations from being applied.
-
Remove the URLs added in
urls.py(Register the Wagtail URLs). -
Remove all Wagtail references added in
base.py(orsettings.py) of your project (Import and configure the package settings).
Testing
Install the requirements from test/requirements.txt and run pytest.
Adding New Tests
Each app has its own tests/ folder, and the blocks also have their own tests/ folder. Be sure to add your new tests in the appropriate location.
The Root tests/ Folder
The package also includes a tests/ folder at the root level. This folder stores general fixtures, helpers, and test mixins that are particularly useful when testing Wagtail features. Please review its contents before adding new tests.
This folder also has an unusual setup, containing its own migrations/ folder and a models.py file. Testing Wagtail requires page models that map blocks, so for this package, the root tests/ folder functions as an app loaded only when running pytest. If you modify any existing block or add a new one, you'll need to update the models in tests/models.py and regenerate its migrations.
Regenerating Migrations for tests/models.py
This app doesn’t require migration history; it only needs to regenerate migration 0001 whenever changes are made to models or blocks within these models. Follow these steps to regenerate it:
- Ensure the package’s Docker instance is running.
- Confirm you’re outside the Docker container and in the package folder (
baseapp-wagtail/). - Run the command
./setup_test_migrations.sh.
That’s all. If it fails, check the .sh file for details and try reproducing its steps in your terminal.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file baseapp-wagtail-1.1.0.tar.gz.
File metadata
- Download URL: baseapp-wagtail-1.1.0.tar.gz
- Upload date:
- Size: 27.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
986125b5e591175e4eb6784c990cf226f48b572392b72502dcf4ba5ad7a83db1
|
|
| MD5 |
5b0d1b6478b9093e5a3e23ab48f24852
|
|
| BLAKE2b-256 |
d8d9902fea7bed395de1417c9dbe64ab97f0a3d87ef2a3df1516e7bf3565e44e
|