Build a blog using the Django Web Framework: Part II
Hello, and welcome to the second part of our series of “Building a blog with Django”. In part one, we talked about setting up our development environment and creating our project. When we ran our server using the “python manage.py runserver” command we got the following output:
It outputs a warning telling us we have unapplied migrations. That has to do with how Django connects with your database (more on that later). For now, let’s focus on taking care of that warning. Run the following command in your terminal. Make sure you are within your virtual environment.
Python manage.py migrate
You should get the following output. Think of your database as a spreadsheet in excel. What that command does for you basically is create rows and columns you might want to populate with data. Again, more on that later. Now that we’ve got that resolved, let’s go ahead and create a super user. Think of a super user as the admin of your site. Run the following command:
Python manage.py createsuperuser
Fill in the fields (email is optional) and provide a secure password. Now we can log into the Django admin with the details we just provided. To do that, run the server from your terminal using the following command:
Python manage.py runserver
You would notice it no longer gives you that warning. Cool. Open your favorite browser, and enter the address localhost:8000/admin. Provide the details of the superuser you just created to log in.
Voila! This is the Django admin page created for us by default. We can create users from this page and assigns to them privileges and a whole lot more powerful stuff. We would be visiting this page all through the course of this tutorial, so it’s a good thing we get comfortable with it now.
Now that we have seen the admin page and what it can do, let’s begin adding flesh and bones to our blog project to get it to that finished product we want. Run the following command in your terminal:
Django-admin startapp blogapp
This creates an app inside our Django project called “blogapp”. A project in Django is a collection of apps (not apps as in mobile phone apps). Think of apps in a Django project as the rooms in a house. The more rooms you have, the bigger the house. The fewer rooms there are in a house, the smaller the house. The rooms make the house a house. It’s probably not the most glamorous analogy, but you get the picture. A project is a collection of apps. To illustrate better, open the project folder. If you named your project “Blog” like I did, you should see another folder inside of it with the same name. Within this sub folder, is a file named “settings.py”. Open it up in your text editor. You should see something like this
“INSTALLED_APPS” indicates the apps currently housed by this project. As you can see, there is an app called “django.contrib.admin”. Remember that admin page we fired up earlier? This is how Django knows it is a part of our project. Cool. Now, we need to add the “blogapp” we just created to the list of installed apps. Your settings file should look like this now:
Don’t forget to add the comma after your app, or Django would throw some nasty errors at you when you try to access it. Next, we want to create the database to hold our blog posts. In Django, that’s very easy to do and it doesn’t involve you writing SQL commands. Django has provided a bit of abstraction so as to make your app more secured. Open your blogapp folder, and open the “models.py” file. Write the following code into it:
A little explanation: What we’ve created is a class in python. This class handles all of our article posts and saves them to a database for us. The database contains a field for the article title, content, the time it was updated and time it was edited. The class also carries a function “__unicode__”. This function returns the title of the article, rather than just an object.
NOTE: If you are writing your code using Python 3x, rather than __unicode__ function, you would use the __str__ function, so your code would look like this:
Great! Next, we want to let Django have a record of the database we just created. To do so, we run the following command in the terminal:
Python manage.py makemigrations
Python manage.py migrate
Cool. Now, we have successfully setup our database, we are ready to begin writing more complex code for our blog app.
What you’ve learned so far:
1. What an app in Django is
2. How to create a super user or admin
3. How to create an app in Django
4. Setting up a database in Django and making migrations
In part three, we would begin creating views for our application. Well done!