Django Tutorial #1: Setup the Project

    Django Tutorial #1: Setup the Project

    More Tutorials on Web Development

    Why Django?

    Django’s primary goal is to ease the creation of complex, database-driven websites. The framework emphasizes reusability and “pluggability” of components, less code, low coupling, rapid development, and the principle of don’t repeat yourself.

    Python is used throughout, even for settings files and data models. Django also provides an optional administrative create, read, update and delete interface that is generated dynamically through introspection and configured via admin models.

    Install Necessary Tools


    Python Programming for Beginners: 103 Examples

    Python is a high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace.

    The language’s design philosophy is summarized as:

    • Beautiful is better than ugly.
    • Explicit is better than implicit.
    • Simple is better than complex.
    • Complex is better than complicated.
    • Readability counts.

    Python’s large standard library, commonly cited as one of its greatest strengths, provides tools suited to many tasks. Django is one of them which is designed for web development.


    PyCharm: the Python IDE for Professional Developers by JetBrains

    PyCharm is an integrated development environment used in computer programming, specifically for the Python language. It is developed by the Czech company JetBrains. Unlike the PhpStorm we talked about in the Laravel tutorial, PyCharm has a community version that is free to use.

    Create a New Django Project

    Open PyCharm and create a new project.

    Make sure the base interpreter is the python.exe you just installed.

    PyCharm will create a virtual environment for the project. A virtual environment makes sure that every Python project uses an independent environment that will not interfere with each other. If you are not using PyCharm, remember to do this manually.

    It would take a few minutes for PyCharm to setup our project. After everything is done, this is what you should get:

    Start the Server

    Now, we can start the development server to test if everything works. Open the terminal, which is located at the bottom left corner if you are using PyCharm.

    It should automatically go to the project root folder and activate the virtual environment. If you are not using PyCharm, you might have to do this manually. Run the following command:

    python runserver

    You’ll see the following output on the command line:

    Performing system checks...
    System check identified no issues (0 silenced).
    You have unapplied migrations; your app may not work properly until they are applied.
    Run 'python migrate' to apply them.
    October 09, 2020 - 15:50:53
    Django version 3.1, using settings 'mysite.settings'
    Starting development server at
    Quit the server with CONTROL-C.

    Open the browser and go to

    Create A Blog App

    Django allows you to create multiple apps in a single project. For example, there could be a “blog” app, a “gallery” app, and a “forum” app inside one single project. These apps could share the same static files, images, videos… or they could be completely independent of each other. Depends on your own need.

    In this tutorial, we’ll create just one “blog” app. Go back to the terminal, and type in:

    python startapp blog

    You should see a new blog folder created in the project directory.

    Now, we need to register this new app with Django. Go to Django_31_Tutorial/ and find INSTALLED_APPS:


    Project Directory

    The Root Directory

    • A command-line utility that lets you interact with this Django project in various ways.

    The Project Directory

    • Django_31_Tutorial/ An empty file that tells Python that this directory should be considered a Python package.
    • Django_31_Tutorial/ As the name suggests, it is the settings/configuration for this Django project.
    • Django_31_Tutorial/ The URL declarations for this Django project; a “table of contents” of your Django-powered site. We’ll talk more about this in the next article.
    • Django_31_Tutorial/ An entry-point for ASGI-compatible web servers to serve your project.
    • Django_31_Tutorial/ An entry-point for WSGI-compatible web servers to serve your project.

    The App Directory

    • blog/migrations: This folder contains all the migration files for the blog app. Unlike Laravel, these files are automatically generated by Django.
    • blog/ Django also comes with an admin panel, and this file contains all the configurations for it.
    • blog/ Models describe the structure and relation of the database. The migration files are generated based on this file.
    • blog/ This is equivalent to the Controllers in Laravel. It contains all the core logics in this app.

    Project Configuration

    Before we dive into our project, there are some changes we need to make to the file.

    Allowed Hosts

    The ALLOWED_HOSTS is a list of domains that the Django site is allowed to serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.

    However, you might notice that even if the ALLOWED_HOSTS is currently empty, we can still access our site using the host That is because when DEBUG is True and ALLOWED_HOSTS is empty, the host is validated against ['.localhost', '', '[::1]'].


    DATABASES is a dictionary containing the settings for the database that our site needs to use. By default, Django uses SQLite, which is a very light weighted database using just one single file. It should be enough for our small project, but it won’t work for large sites. So, if you want to use other databases, here is an example:

        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'mydatabase',
            'USER': 'mydatabaseuser',
            'PASSWORD': 'mypassword',
            'HOST': '',
            'PORT': '5432',

    Static and Media Files

    And finally, we need to take care of the static and media files. Static files are CSS and JavaScript files, and media files are images, videos and other things the user might upload.

    Static Files

    First, we need to specify where these files are stored. For our Django site, we’ll create a static folder inside the blog app. This is where we store static files for the blog app during development.

    In the production environment, however, things are a little different. We need a different folder under the root directory of our project. Let’s call it staticfiles.

    Then we specify this folder in the

    STATIC_ROOT = 'staticfiles/'

    Next, we need to tell Django what URL to use when accessing these files in the browser. It does not have to be /static, but do make sure it does not overlap with our URL configurations which we’ll talk about in the next article.

    STATIC_URL = '/static/'

    Media Files

    Media files are configured in the same way. We create a mediafiles folder in the project root directory:

    And then, we specify the location and URL:

    MEDIA_ROOT = 'mediafiles/'
    MEDIA_URL = '/media/'

    In the next article, we’ll talk about the URL configurations in Django.

    Leave a Reply

    Your email address will not be published. Required fields are marked *