Django is a versatile, extremely fast, very secure, and easily scalable Python Web framework, with many extra feature right out of the box, like: user authentication, content administration, etc.


$ sudo apt-get install git
$ sudo apt-get install python3
$ sudo apt-get install python3-pip
$ sudo pip install virtualenv


$ mkdir project
$ cd project
$ virtualenv --python=/usr/bin/python3 env

You can activate it.

$ source ./env/bin/activate

Verify the version of python from the first line by the below command.

$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

When not using you can deactivate it.

$ deactivate

Django project

$ pip install django
$ python -m django --version

Django Installation

$ pip freeze > requirements.txt
$ pip install -r requirements.txt
$ django-admin startproject website
$ cd website
$ python migrate
$ python runserver

Django Project initial structure

$ python runserver 0:8000

Then go to http://localhost:8000.

Django Running migrations


Let’s use git to save the current changes.

$ vim .gitignore
$ git init
$ git add .
$ git commit -am "Initial commit"

Django Blog app

Now let’s add a blog.

$ python startapp blog
$ python makemigrations blog
$ python migrate
$ python shell
$ python createsuperuser

Project URLS

$ vim website/
from django.conf.urls import include, url
from django.contrib import admin
from django.urls import path

urlpatterns = [
    url(r'^', include('blog.urls')),

Project settings

$ vim website/
# Application definition



$ vim blog/
from django.conf.urls import url
from blog import views

urlpatterns = [
    url(r'^blog/(?P<pk>[0-9]+)/$', views.ShowView.as_view(), name='post-show'),
    url(r'^$', views.IndexView.as_view(), name='blog'),

App views

$ vim blog/
from django.views import generic
from django.shortcuts import get_object_or_404
from blog.models import Post

class IndexView(generic.ListView):
    model = Post
    template_name = "index.html"
    context_object_name = "posts"
    queryset = Post.objects.all()

class ShowView(generic.DetailView):
    model = Post
    template_name = "show.html"
    context_object_name = "post"

    def get_object(self, queryset=None):
        pk = self.kwargs.get(self.pk_url_kwarg, None)
        return get_object_or_404(Post, pk=pk)

App models

$ vim blog/
from django.db import models
from django.urls import reverse

class Post(models.Model):
    title = models.CharField(max_length=150)
    author = models.CharField(max_length=150)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    modified_at = models.DateTimeField(auto_now=True, editable=False)

    def get_absolute_url(self):
        return reverse('post-show', args=[str(])

App templates

$ mkdir blog/templates
$ vim blog/templates/base.html
<!DOCTYPE html>

        <meta charset="utf-8">
        {% block content %}{% endblock %}
$ vim blog/templates/index.html
{% extends 'base.html' %}
{% block content %}
  <h1>Blog posts</h1>
  {% for post in posts %}
    <div><a href="{{ post.get_absolute_url }}">{{ post.title }}</a> by {{ }}</div>
  {% endfor %}
{% endblock %}
$ vim blog/templates/show.html
{% extends 'base.html' %}
{% block content %}
        <h1>{{ post.title }}</h1>
        <h2>by {{ }}</h2>
        <div>{{ post.content }}</div>
        <a href="/">Back</a>
{% endblock %}

Final steps

$ python migrate
$ python runserver

Django New model migration

Then go to http://localhost:8000.

$ vim blog/
from django.contrib import admin
from .models import Post

class BlogAdmin(admin.ModelAdmin):

Django Admin with new Post model

$ git commit -am "Add blog"

Thank you for reading!