provocationofmind.com

How to Create a CMS Using Django and Bulma CSS Framework

Written on

In this guide, you will learn how to construct a content management system (CMS) using Django, allowing users to:

  • Add new posts
  • View all posts
  • Edit existing posts

First, create a project directory and navigate into it:

mkdir CMS

cd CMS

Next, set up and activate a virtual environment:

python3 -m venv env

source env/bin/activate

Now, create a Django project named django_cms and within it, create an application called cms:

django-admin startproject django_cms

cd django_cms/

django-admin startapp cms

Install Django within your virtual environment:

pip install Django

Add the cms app to the INSTALLED_APPS list in your settings.py file:

INSTALLED_APPS = [

'cms',

]

Start the development server to verify that everything is functioning correctly:

python3 manage.py runserver

Visit http://127.0.0.1:8000/ to check if the server is running properly.

Django project overview

Create Models

In the cms/models.py file, include the necessary code to define your models.

Apply Migrations

Run migrations to generate the corresponding database tables:

python3 manage.py makemigrations

python3 manage.py migrate

Django Forms

One of Django's benefits is its provision of classes that simplify many tasks. For instance, the ModelForm class allows for straightforward form creation from models.

To create a form for data submission, add a forms.py file in the cms app and include the following code.

Here, we define a form that extends ModelForm and specifies which fields to include. Additionally, we adjust the properties of the textarea for content submission to be larger, i.e., 100 columns and 30 rows.

Form Rendering

Next, render the form in the view. Open views.py and add the corresponding code.

First, we instantiate the form and pass it to the templates.

As the template is currently nonexistent, we need to create it. Django looks for templates in a templates directory, so create base.html and stories.html as follows:

cms/

templates/

  • base.html
  • stories.html

Bulma CSS

Bulma is a CSS framework that aids in styling your HTML. To begin, utilize the starter template found here. Open base.html and incorporate the necessary code.

All templates will derive from this base template. Now, open stories.html and render the previously created form.

URLs

Add a urls.py file to the cms app and insert the following code to serve the view:

from django.urls import path

from .views import add_stories

urlpatterns = [

path('add', add_stories, name="add_story"),

]

After running the server, navigate to http://127.0.0.1:8000/add to view your page.

Add story page

Retrieve Data from Form

Now, let's enhance the view to include the logic for obtaining data from the form and saving it to the database. Open views.py and modify the stories function accordingly.

Upon creating a new post, the user will be redirected to a page displaying all stories. We will create a view to retrieve and show the posts.

Next, add a new HTML file named story_list.html to display all posts. Update the URL accordingly.

The page now appears as follows:

Story list page

The final step involves enabling post editing. Open views.py and insert the following code:

In this section, we utilize UpdateView, a class-based view that streamlines the updating process. It requires the following attributes:

  • model
  • fields
  • template_name
  • success_url

The form allows for updates on the specified fields. Next, create the necessary template to render the form.

Update the urls.py file to serve this view. Your urls.py should now resemble the following:

The page for updating a post will look like this:

Edit post page

Conclusion

This tutorial has covered essential concepts required to build a fully functional Django web application. The complete code is available here.

If you are interested in adding custom authentication features, refer to these articles.

  • How to Add Social Authentication to a Django Application

    • Add Twitter sign-in to a Django application
    • python.plainenglish.io
  • How to Create a Custom Authentication System in Django

    • The first step in developing dynamic sites
    • betterprogramming.pub

For more content, visit PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Check out our Community Discord and join our Talent Collective.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Embracing Uncertainty: Discovering Strength in the Unknown

Explore the journey of finding strength amidst uncertainty and the magic of embracing the unknown.

The Perspective of Impersonal Energy: Understanding Trauma

Explore the impact of trauma and how to perceive energy through a lens of understanding and compassion.

Remembering Daniel Kahneman's Legacy in UX Design

Reflecting on Daniel Kahneman's insights from

Can Giant Viruses Help Preserve Greenland's Glaciers?

Scientists explore the role of giant viruses in combating ice melt in Greenland, revealing unexpected interactions in glacial ecosystems.

Exploring the Mysteries of Our Existence: Dots and Connections

A deep dive into the complexities of understanding the mysteries of our existence, exploring how we connect the dots in our reality.

Harnessing Offshore Hydrogen: A New Era in Clean Energy Production

The UK prepares to produce its first offshore hydrogen, joining a global effort towards economically viable clean energy solutions.

Revitalize Your Writing Career: 5 Essential Steps to Take

Discover five crucial steps to assess and revive your writing career.

Navigating the Complexities of Temu and Ethical Shopping

Examining Temu's low prices and their potential ethical implications in the context of labor exploitation.