Skip to content

Commit 7cb604d

Browse files
aclark4lifeCopilot
andcommitted
Add django-setup recipe to run migrations and create Wagtail initial data before serve
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c56f9e4 commit 7cb604d

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

  • src/dbx_python_cli/templates/project_template

src/dbx_python_cli/templates/project_template/justfile

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,36 @@ pip-install:
6060
python -m pip install -e .
6161
alias i := pip-install
6262

63-
serve: npm-install npm-serve django-serve
63+
# Run migrations and create Wagtail initial data (root page, home, site, superuser)
64+
[group('django')]
65+
django-setup:
66+
#!/usr/bin/env sh
67+
export DJANGO_SETTINGS_MODULE={{ project_name }}.settings.{{ project_name }}
68+
python manage.py migrate
69+
python -c "
70+
import os, django
71+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings.{{ project_name }}')
72+
django.setup()
73+
try:
74+
from wagtail.models import Page
75+
if not Page.objects.filter(depth=1).exists():
76+
from django.conf import settings as s
77+
from django.contrib.contenttypes.models import ContentType
78+
from wagtail.models import Locale, Site
79+
lang = (getattr(s, 'LANGUAGE_CODE', 'en') or 'en').split('-')[0][:2]
80+
locale, _ = Locale.objects.get_or_create(language_code=lang)
81+
ct, _ = ContentType.objects.get_or_create(app_label='wagtailcore', model='page')
82+
root = Page.add_root(title='Root', slug='root', content_type=ct, locale=locale)
83+
home = root.add_child(title='Home', slug='home', content_type=ct, locale=locale)
84+
if not Site.objects.exists():
85+
Site.objects.create(hostname='localhost', root_page=home, is_default_site=True)
86+
print('Wagtail root page and site created.')
87+
except ImportError:
88+
pass
89+
"
90+
export DJANGO_SUPERUSER_PASSWORD=admin && python manage.py createsuperuser --noinput --username=admin --email=admin@example.com || true
91+
alias setup := django-setup
92+
93+
serve: django-setup npm-install npm-serve django-serve
6494

6595
alias s := serve

0 commit comments

Comments
 (0)