Create a remarkable website in 10 minutes using Wagtail. Learn Python/Django setup, Wagtail installation, dependency management, admin user creation, custom views, and more. Swift website building made easy.
Create a remarkable website in 10 minutes using Wagtail. Learn Python/Django setup, Wagtail installation, dependency management, admin user creation, custom views, and more. Swift website building made easy.
pip
.pip
.requirements.txt
.urls.py
Fileviews.py
blog_list
) using Wagtail's Page
model.base.html
) with common structure.blog_list.html
) using template inheritance.Page
model to retrieve page count.settings.py
INSTALLED_APPS
.WAGTAIL_SITE_NAME
for site identification.Step 12: Running the Development Server
In the fast-paced world of web development, creating dynamic and captivating websites is crucial. Wagtail, an open-source content management system (CMS) built on Django, empowers developers to effortlessly craft feature-rich websites. This comprehensive guide takes you through building an extraordinary website using Wagtail in just 10 minutes. With detailed steps, examples, cross-platform adaptability, and Django integration, you'll create an online presence that leaves a lasting impact.
Before diving into Wagtail, ensure you have:
Python Compatibility Check: Wagtail relies on Django. First, check if Python is compatible:
python3 --version
Output (example):
Python 3.9.6
Ensure Python is within the supported versions for Wagtail.
Django Installation: Install Django:
pip install django
Output (example):
Collecting django
...
Successfully installed django-3.2.6
Creating a Virtual Environment:
If you're on Windows (cmd.exe), execute the following commands:
py -m venv mysite\env
mysite\env\Scripts\activate
Output:
(env) C:\path\to\your\project\mysite>
If you're on GNU/Linux or MacOS (bash), type:
python3 -m venv mysite/env
source mysite/env/bin/activate
Output:
(env) $ /path/to/your/project/mysite$
Note: The (env)
at the beginning of your terminal prompt indicates the virtual environment is active.
Wagtail Installation:
With your virtual environment active, install Wagtail:
pip install wagtail
Output:
Collecting wagtail
...
Successfully installed wagtail-2.14.1
Generate Project Structure:
Use Wagtail's generator to create a project structure:
wagtail start mysite mysite
Output:
...
Created Wagtail project 'mysite'
...
Project Structure:
mysite/
├── .dockerignore
├── Dockerfile
├── home/
├── manage.py*
├── mysite/
├── requirements.txt
└── search/
The project structure is now set up.
Navigate to Project Directory:
cd mysite
Install Dependencies:
Install the project dependencies listed in requirements.txt
:
pip install -r requirements.txt
Output :
Collecting psycopg2-binary==2.9.1
...
Successfully installed psycopg2-binary-2.9.1
All required dependencies are installed.
Evolve Database:
Run the migration command to evolve the database:
python manage.py migrate
Output:
Operations to perform:
...
Applying sessions.0001_initial... OK
The database has been created and migrated successfully.
Create Superuser:
Forge a superuser account to wield administrative power:
python manage.py createsuperuser
Output:
Username: admin
Email address: admin@example.com
Password:
Password (again):
Superuser created successfully.
An admin superuser has been created.
urls.py
FileURL Patterns:
In your project's urls.py
file, URL patterns are defined. These patterns map URLs to corresponding views.
from django.contrib import admin
from django.urls import path
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('cms/', wagtailadmin_urls),
path('documents/', wagtaildocs_urls),
]
URL patterns have been configured.
views.py
Custom View:
In your app's views.py
file, you can craft custom views to handle
requests. Here's an example:
from django.shortcuts import render
from wagtail.core.models import Page
def blog_list(request):
blog_pages = Page.objects.filter(content_type__model="blogpage")
return render(request, 'blog/blog_list.html', {'blog_pages': blog_pages})
A custom view, blog_list
, has been created.
Template Directory:
In your project directory (mysite
), create a directory named templates
.
Creating a Base Template:
Inside the templates
directory, create a subdirectory named base
and within that, create a file named base.html
.
<!-- templates/base/base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}My Wagtail Site{% endblock %}</title>
</head>
<body>
<header>
<h1>{% block header %}My Wagtail Site{% endblock %}</h1>
</header>
<nav>
<ul>
<li><a href="{% url 'home' %}">Home</a></li>
<!-- Add more navigation links as needed -->
</ul>
</nav>
<main>
{% block content %}
{% endblock %}
</main>
<footer>
<p>© {% now 'Y' %} My Wagtail Site</p>
</footer>
</body>
</html>
Creating a Blog List Template:
Still within the templates
directory, create a subdirectory named blog
and within that, create a file named blog_list.html
.
<!-- templates/blog/blog_list.html -->
{% extends 'base/base.html' %}
{% block title %}Blog List - My Wagtail Site{% endblock %}
{% block header %}Blog List{% endblock %}
{% block content %}
<ul>
{% for page in blog_pages %}
<li><a href="{% page.url %}">{{ page.title }}</a></li>
{% endfor %}
</ul>
{% endblock %}
ORM Query Checks:
Let's perform some checks using Django's ORM. In your virtual environment, run:
python manage.py shell
In the Python shell:
from wagtail.core.models import Page
page_count = Page.objects.count()
print("Total Pages:", page_count)
Output:
Total Pages: <count_value>
This confirms the ORM's functionality.
settings.py
Wagtail Configuration:
Open your settings.py
file and add Wagtail's configuration:
INSTALLED_APPS = [
...
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
...
]
Make sure you include the required Wagtail apps.
Update the MIDDLEWARE
section:
MIDDLEWARE = [
...
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
...
]
This enables redirects in Wagtail.
Add the Wagtail settings at the end of the settings.py
file:
WAGTAIL_SITE_NAME = 'My Wagtail Site'
Customize WAGTAIL_SITE_NAME
as desired.
With your virtual environment active and inside your project directory, run the development server:
python manage.py runserver
Output (example):
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Open your web browser and go to http://127.0.0.1:8000/ to see your Wagtail site in action.
By following these steps, you've gained a solid foundation in setting up a Wagtail project. You've learned how to install prerequisites, create a virtual environment, install Wagtail, generate a project structure, manage dependencies, evolve the database, create an admin superuser, understand URL patterns, craft custom views, perform ORM checks, configure Wagtail in your project's settings.py
, and create templates for your website. Armed with this knowledge, you're ready to unleash your creativity and build captivating websites with Wagtail.
DigitalOcean Sign Up : If you don't have a DigitalOcean account yet, you can sign up using the link below and receive $200 credit for 60 days to get started: Start your free trial with a $200 credit for 60 days link below: Get $200 free credit on DigitalOcean ( Note: This is a referral link, meaning both you and I will get credit.)
👩💻🔍 Explore Python, Django, Django-Rest, PySpark, web 🌐 & big data 📊. Enjoy coding! 🚀📚