Sometimes it is more of an effort to
adapt an existing CMS to your needs than it is to build one from scratch
As many wise people will tell you, avoid
making your own CMS, make modules for an existing open source CMS instead. The
are many reasons for this more work, less people who understand your code, poor
security. Yet, with the right framework all this can be avoided. Content
Management Systems usually revolve around the kind of content being managed,
blog posts / articles, audio, video, images or something else entirely. If the
data has a non-standard hierarchy then a traditional CMS system might not do.
In
this DIY we will use Django, a popular web framework written in Python. In this
example, we will make a movie database application.
Imagine a CMS for car parts that has a
hierarchy of part, which could fit in any number of car, and which could work
with any number of other parts. It is unlikely you will find something exactly
for this.
So how about we make our own? In this DIY
we will use Django, a popular web framework written in Python. In this example,
we will make a movie database application. Here we have a movie, and each movie
has people attached to it in different roles, director, producer, actor and so
on.
If you are familiar with coding you should
be able to follow this DIY quite easily.
Pre-requisites
To make this application, you need to have
Python and Django installed. Rather than go through that here, we can point you
to a good tutorial on the Django website:
https://docs.djangoproject.com/en/dev/intro/install/
Next we need to set up our project. Create
an empty directory for your projects files, and in it run the following
command:
django-admin.py startproject mycms
We just created the basic structure of a
Django project; a project in turn can include one or more “apps”. We create
that as follows:
python manage.py StartApp movies
We created a single app, called “movies”.
An app provides a distinct bit of functionality. In a CMS, one app might manage
articles, another the forum and a third could handle images and media. Apps can
reference one another, so you could embed your media into a forum post or
article.
The Django Project Structure
A Django project is a collection of apps
configured to work together. You can write these apps, or find ready-made apps
and integrate them into your own project.
A
Django project is a collection of apps configured to work together.
At this point we have the basic structure
of our project, so let’s see what role some of the important files play:
1. manage.py: You use this to run commands on your project.
2. myCmsFolder: This folder has files related to your Django project.
3. mycms/settings.py: This is the file that has all the settings for
the project.
4. mycms/urls.py: This file has a table of contents that maps URLs to
features of our web app.
5. movies folder: This folder has files related to your movies app.
6. movies/models.py: Here we define the data structure of our website.
7. movies/views.py: This file has the code that actually returns HTML
code to a user browsing the website.
Basic setup
We need to change some very basic settings
before this project will work the way we want. First let’s set up the database;
open settings.py and look for the code beginning with DATABASES, it should be
near the top.
Here, change the ENGINE parameter to
django.db.backends.sqlite3and set NAMEtotest.db.SQLite is a simple SQL engine
that keeps everything in a file so we won’t need to set up a server or anything.
Further down, in the INSTALLED_APPS code
section, uncomment the line’s first commented entry “django.contrib.admin” by
removing the # and the space after it; remember, indentation matters a lot in
Python.
We will also add our own app to the list of
installed apps. Add the following line of code at the end inside the
INSTALLED_APPSblock:‘movies’,
Like the previous step, mind the
indentation.
Finally, open the urls.pyfile in the myCmsFolder
and uncomment the lines for the “admin” as instructed by the comments on that
file.
Giving some structure to our blog
Now let’s start actually building our CMS.
We will start with our models file that defines the data structure of our app.
We
will have three kinds of objects in our database, the first is a Person object.
We will have three kinds of objects in our
database, the first is a Person object. This is just a first and last name.
Second, we have a Role object, for the role a person plays in a movie; just a
name is enough here. Finally we have a Movie, which has a title, and an
arbitrary number of people, with different roles.
class Person(models.Model):
first_name =
models.CharField(max_length=50)
last_name = models.CharField(max_length=50)def_unicode_(self):
return “%s %s” % (self.first_name, self.last_name)
The Person class is sub-classing the Model class
provided by Django, which ensures that we get all the benefits of Django
automatically. This will automatically create a database table and provide a
rich API. We have kept the person model simple, just the name, but we could add
a whole lot more. Each of these becomes a column in the database table, in this
case a character field with a maximum length of 50 characters.
The _unicode_method is meant to return a
textual representation of this object; here that is a combination of the first
and last name.
What we need next is a Roleclass to define
the possible roles a person can have. Rather that hardcoding it, we leave it
flexible, so that more roles can be added in the future.
class Role(models.Model):
name = models.CharField(max_length=50)
def _unicode_(self):
return self.name
This is pretty straightforward compared to
what we already did, if you noticed.
Finally, we need a @Movie@ class for each
movie; we will keep this simple as well:
class Movie(models.Model):
title = models.CharField(max_length=256)
summary = models.TextField()
release_date = models.DateField()
def _unicode_(self):
return self.title
Here we have two new fields, a Text Field
which allows for a greater bulk of text, and a Date Field for the release date.
Although now we have all the basic data, we
need some way to connect them, a way to say which Person worked on what Movie
in what Role. We’ll call this data model Credit; here is how we define that:
class Credit(models.Model):
movie = models.ForeignKey(Movie)
role = models.ForeignKey(Role)
person = models.ForeignKey(Person)
Foreign Key is used to link one kind of
data to another. So a blog post can have a Foreign Key to an author, or a
product object can have one for a manufacturer; it is necessary to create some
kind of hierarchy.
With this we have defined the structure of
the data our app will work with, but we still need to tell it how everything
works together.
Before we do that though, let’s have Django
create the basic structure for our database; run the following command in the
main project folder:
python manage.py syncdb
This creates database tables for our models
and allow you to set up an admin username and password; keep note of that.