Code a simple blog using Python and Django.

Django Blog | How to Make Your First Blog with Django

For anyone looking to code their own blog, Django is a useful tool for those who like to code using Python. As such, Django can essentially be used as building blocks to eliminate guesswork and save development time. In this post, we will explore the advantages of Django, how to utilize Django on a self-hosted VPS, and how to make a blog with Django.

What is Django?

Django is an open-source web framework, written in Python, that follows the model-view-template architectural pattern. Consequently, a web framework is just a set of components that help you to develop websites faster and easier. Basically, it is pre-made snippets of code, that have already been tested and debugged, that can be used alongside your own code. This allows for quick and easy setup of complex and intricate code without having to worry about developing the base or framework.

When building a website, one always needs a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Luckily, other people long ago noticed that web developers face similar problems when building a new site, so they teamed up and created frameworks (Django being one of them) that offer ready-made components to use.

Frameworks exist to save developers from having to reinvent the wheel and to help alleviate some of the overhead when building a new site using Django.

What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. In other words, it is just another programming language and one of many options that developers have when looking at building a new site or web app. A few famous examples of websites built with python include Google, YouTube, DropBox, and Reddit. The biggest advantage that python has above all other programming languages and options is its speed. As a result, Python 3.5 typically comes in at almost a full THIRD of the time or faster, than a PHP 5.6 website would!

Python is an extremely versatile and powerful object-oriented language at that, which means that it can be used to make some pretty powerful web apps and programs.

Advantages of a Django Blog or Django Web App

The biggest advantage of using Django to make a blog or web app is pretty clear, it’s speed. However, that speed is two-fold when dealing with Django. First, it is in development, as you don’t need to reinvent the wheel, you will find that you just breeze through many of the cumbersome and time-consuming parts of development. Your new site or app will be able to soar farther and higher than ever before because it was built on the backs of giants. Most, if not all, of the work you’d be doing, will have already been done AND tested for you when you choose to work within a framework.

Second, speed in performance. Django is based in Python, which has time and time again outperformed both Ruby and PHP. In today’s day of mobile websites and apps, it is important that you are relaying data as quickly as possible with making as few and lightweight server calls as possible. You want to be sure that your clients don’t waste both time and precious data off their plans, just trying to load your website.

Django Tutorial: How to make a Blog with Django

How to make a Blog with Django.. Screenshot source: django-planet

Step 1: Installing Django

First login to your SkySilk dashboard and select “Create Container” then just select a plan.

 

When your plan is selected, choose a hardware configuration, or configure your own custom hardware.

Once you have your hardware configured, select “Apps and Tools” then find “Django” and select it.

Finally, just enter the required info and hit “Go!”

READ: Advantages to Turnkey Linux Applications on a SkySilk Linux VPS

Step 2: Starting The Application

Navigate to your project directory, this should contain a __init__.py, manage.py, settings.py and urls.py. There may also be a few others. For reference, my project name is djangorocks, you will need to replace any references to djangorocks throughout this tutorial.

In addition, type the following into terminal; this creates a new application folder & key files we will be using for our blog:

python manage.py startapp blog

As a result, you should now have a folder called blog containing 3 files: __init__.py, models.py and views.py.

Important:

Open up your settings.py, this is located in your project folder. Find INSTALLED_APPS and add ‘blog’. Consequently you should have something similar to the following;

Django now knows about your new application, all that is left to do is create it.

Step 3: Define Your Models

The model is your database structure. Let’s start by opening the models.py file, and start adding some fields. Because this is meant to be a simple quide, it will not be including users at this stage.

This creates a database table with the name “Blog”. As a result, this need to be something obvious and will be used a lot.

These are basic fields to be created in your database:

The last field, a little more advanced. This field populates its data from another database table, in this case, Category, so you will need to populate the Category table field first.

Finishing Up

So, there are a few more things to be added to the model now that we have decided on the database structure. Your completed model file should look as follows.

What are these extras for?

The __unicode__ function sets the text reference for each record. This is used mainly in the automated django admin, but this is still available to use on your own site.

The get_absolute_url function defines a URL, again used in the admin area, for each record.

Without the @permalink decorator, the following would not work. This returns a URL calculated from the urls.py file which will be explained shortly. I would recommend using this method as it allows you to change the URL for a page in only one location.

Example

Title: How to make a blog with Django

Response from __unicode__: How to make a blog with Django

Response from get_absolute_url: /blog/view/how-to-make-a-blog-with-django.html

Step 4: Configure the Auto-Admin

In many of your own applications, you will probably want to write your own administration functions:

Create admin.py in the blog folder we created earlier. This admin.py file is automatically checked by Django admin for every application defined under INSTALLED_APPS in the settings.py

Import the command which allows us to register the model we created

Also, Import our models

Register our models Blog & Category with the admin

Although these three lines are enough to get the admin working, we want to add a little more functionality. Heres the final admin.py.

Now that you have added these models into the admin, you might want to log in and add a few categories and blog posts.

Step 5: Writing The Views

The view is where you do all the logic to be sent to your templates. In this example, we will not be dealing with RequestContext. As a result, this would give you access to the request object which contains details of the currently logged in user, as well as a few other features you will be likely to use in the future.

For this example, we need to create 3 views.

  • Display your categories & latest posts
  • Display the posts in a specific category
  • Display the post

Here is a copy of the view.py.

As with the admin.py file we need to import the models.

We also need to import a couple of functions for displaying our template.

The following is for your index page, which will display a list of all your categories and 5 most recent posts.

When defining functions, you always need to specify the variable request. This is the request object, which contains details of the User, POST & GET data as well as a few other bits. In the following instance, we are also specifying slug which is mapped to from the urls.py file as you will see in a minute

This first part sets the template file that we are going to be using.

The next part queries the database for both categories and posts. Category and Blog are the names of the models we created earlier.

In the other two functions view_post & view_category we use one of the rather useful Django shortcuts. This queries the database trying to match where slug=slug, the first slug being the field in the model, the second slug being the input into the function call, more on this in a second when we define the URLs.

Step 6: Defining URLs

Open up the urls.py located in your project folder, add the following 3 lines.

The first URL match is just a simple match nothing, ie http://www.yourdomain.com/, and mapping that through to the view index. The second two have custom variables being passed to the view. These are just regular expressions with the parameter matching the syntax of Django.


Using the .html extension on URLs should work fine. This matches everything up to ‘.’, there are other ways of doing this, and it maps the result to slug which is also the name of a parameter in the view_post and view_category functions.

The final part labeled name is what we used when defining the models. get_absolute_url returns a URL automatically calculated based on the URL that is entered here. Defining this just once means that if you change the mapping URL, it will also change throughout the site. You are also able to use the template tag that Django provides to do a similar thing, however, this is not used in our example.

Step 7: Templating

The templating system in Django is extremely powerful, as a result, we will only be using a small handful of its functionality in this example.

First of all, let’s create our base template. This is just a very basic example, which is not valid XHTML. This template alone doesn’t do a great deal.

So, In the base.html we are defining the base content, <html>, <body> etc, as well as the blocks we would like to display. Therefore, the content for these is output based on the content that extends the base template.

On the first line of all three of the other files, you will notice

This line calls the base.html template file. Inside this we define the blocks very much the same way as in the base.html file, however this time we add the content we want to display. Using the example from index.html

We define the block for content:

Inside here we are checking if there are any categories. Furthermore, the variables we use here were defined in the views.pyfile in the render_to_response function.

If there are any categories, then the following is run:

Finally, his will loop the response from the Category model as defined in views.py, and print out the results that are entered into the database.

How to make a Blog with Django: Conclusion

Congratulations! You have made it all the way through the tutorial, and you should now be able to answer questions like:

What is Django?

What is Python?

What is a Web Framework?

How to Make a Blog Using Django?

With this knowledge, you can begin coding a blog with Django as the base of your web framework. Especially relevant, with a self-host SkySilk Linux VPS and Turnkey Django Template, you can deploy the exact resources you need for the project scope in a matter of second and furthermore, scale them as needed.

Deploy a Self Hosted Django VPS

(Visited 4,843 times, 9 visits today)

Share this post with your friends

Share on facebook
Share on twitter
Share on linkedin