How to Build a Website with Wagtail in 10 Minutes

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.

image description

How to Build a Website with Wagtail in 10 Minutes

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.

Introduction:

  • Importance of dynamic and captivating websites in web development.
  • Introduction to Wagtail, an open-source CMS built on Django.
  • Empowerment of developers to create feature-rich websites.
  • Overview of the guide's purpose and content.

Prerequisites:

  • Explanation of the importance of prerequisites.
  • Detailed explanation of the required steps before starting with Wagtail.

Step 1: Python and Django Installation

  1. Compatibility check for Python version.
  2. Installation of Django using pip.

Step 2: Virtual Environment Setup

  1. Creating a virtual environment for project isolation.
  2. Steps for creating a virtual environment on Windows and Unix-like systems.
  3. Activation of the virtual environment.

Step 3: Installation and Setup

  • Explanation of setting up Wagtail and the project structure.

Step 3.1: Install Wagtail

  1. Installation of Wagtail using pip.

Step 3.2: Generate Project

  1. Generating the project structure using Wagtail's generator.
  2. Explanation of the generated project structure.

Step 4: Install Project Dependencies

  1. Navigating to the project directory.
  2. Installing project dependencies from requirements.txt.

Step 5: Create the Database

  1. Evolving the database using migration.
  2. Explanation of the migration process.

Step 6: Create an Admin Luminary

  1. Creation of an admin superuser for administrative control.
  2. Step-by-step process of creating a superuser.

Step 7: Understanding the urls.py File

  1. Explanation of URL patterns in Django.
  2. Configuration of URL patterns for admin and documents using Wagtail.

Step 8: Crafting a Basic View in views.py

  1. Creation of custom views to handle requests.
  2. Example of a custom view (blog_list) using Wagtail's Page model.

Step 9: Creating Templates

  1. Explanation of the importance of templates in rendering web pages.
  2. Creation of a base template (base.html) with common structure.
  3. Creation of a blog list template (blog_list.html) using template inheritance.

Step 10: ORM Checks and Sample Execution

  1. Performing checks using Django's ORM in a Python shell.
  2. Example of querying the Page model to retrieve page count.

Step 11 Configuring Wagtail in settings.py

  1. Introduction to Wagtail-specific settings in INSTALLED_APPS.
  2. Explanation of adding middleware for redirects.
  3. Configuration of WAGTAIL_SITE_NAME for site identification.

Step 12: Running the Development Server

Conclusion:

  • Recap of the journey through the steps.
  • Empowerment of readers to build captivating websites using Wagtail.
  • Encouragement to explore further and unleash creativity.

Introduction:

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.

Prerequisites:

Before diving into Wagtail, ensure you have:

Step 1: Python and Django Installation

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

  2. Django Installation: Install Django:

    pip install django

    Output (example):

    Collecting django
    ...
    Successfully installed django-3.2.6

Step 2: Virtual Environment Setup

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

Step 3: Installation and Setup

Step 3.1: Install Wagtail

  1. Wagtail Installation:

    With your virtual environment active, install Wagtail:

    pip install wagtail

    Output:

    Collecting wagtail
    ...
    Successfully installed wagtail-2.14.1

Step 3.2: Generate Project

  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.

Step 4: Install Project Dependencies

  1. Navigate to Project Directory:

    cd mysite
  2. 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.

Step 5: Create the Database

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

Step 6: Create an Admin Luminary

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

Step 7: Understanding the urls.py File

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

Step 8: Crafting a Basic View in views.py

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

 Step 9: Creating Templates

  1. Template Directory:

    In your project directory (mysite), create a directory named templates.

  2. 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>&copy; {% now 'Y' %} My Wagtail Site</p>
        </footer>
    </body>
    </html>
  3. 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 %}

 

Step 10: ORM Checks and Sample Execution

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

Step 11: Configuring Wagtail in settings.py

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

Step 12: Running the Development Server

Run Server:

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.

Conclusion:

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 Referral Badge

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


Latest From PyDjangoBoy

👩💻🔍 Explore Python, Django, Django-Rest, PySpark, web 🌐 & big data 📊. Enjoy coding! 🚀📚