Introduction
Referring to my previous article, we already set up the development environment for Django but I have this feeling of being at the beach with my feet still dry. So let’s get to the fun part and create our first project.
Before actually creating our first project let me introduce you to the boss of Django, “django-admin”. The reason I’m calling it a boss is because this command-line utility is at the heart of Django's project management facilities. You’ll need this utility to perform a bunch of administrative tasks including:
- Creating a new project.
- Starting the development web server.
- Creating and managing the project's database.
- Validating the current project and testing for errors.
Now, let’s get started.
Creating a new project
To create your project, open command prompt and navigate to the location on your drive where you want to create the project. Now, type the following command and hit enter.
- django-admin startproject website
- # website is the name of project. You can choose any name for your project.
What this command does is, it will make a folder name ‘website’ in the current directory. This folder also contains an initial directory structure for our project inside it.
Now that our project is created, before exploring the content of this folder; let’s take a moment and talk about the IDEs.
Python is a simple language and a plain text editor such as Vim, Emacs, or Sublime Text work fine but they simply focus on the editing of a single file which is lean-and-mean but when it comes to developing complex applications then it’s quite frightening for me to even think about text editors. (Ok, I never ever used a text editor. Not a fan.). Whereas the Integrated Development Environment (IDE) has a broad vision. They look at the whole project at once and unify all coding related activities. IDEs also have plus points like Auto-completion, quick syntax error fixes, code navigation and even smart assistance addressing the semantics of your code.
There are a bunch of IDEs to choose from. Some of the most commonly used IDEs are,
- PyCharm by JetBrains
- Visual Studio Code by Microsoft
- PyDev for Eclipse
Personally, I’ve been using PyCharm and I love it. It seems friendlier to me, easy to install and use. For the rest of the article, I’ll be working with PyCharm. Things are not so different for any IDE you choose so no worries, pal.
Project Exploration
Let’s continue where we left, open your newly created project. The first file you’ll come across is manage.py.
manage.py
This file would be created automatically every time you create a project. It can be thought of performing the same thing as django-admin but just for this project. If you open this file with any text editor or IDE, the code just looks something like this.
- #!/usr/bin/env python
- import os
- import sys
- if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings")
- try: from django.core.management
- import execute_from_command_line
- except ImportError as exc: raise ImportError("Couldn't import Django. Are you sure it's installed and "
- "available on your PYTHONPATH environment variable? Did you "
- "forget to activate a virtual environment?") from exc
- execute_from_command_line(sys.argv)
Next, open the subfolder with the same name that of your project. Let’s go through each file one by one.
__init__.py
Assuming you have the basic knowledge of python, don’t merely think of this special __init__ method to be the constructor for python. If you open it you’ll see that IT DOESN’T CONTAIN ANY CODE but its presence tells python that this whole folder is to be treated as a package. Technically, Django projects are no more than python packages.
setting.py
This is the main configuration file of your project; generally speaking; including all the settings needed for your project to run successfully. Let’s go through the sections of this file one by one.
- '''''
- Django settings for a website project.
- Generated by 'django-admin startproject' using Django 2.0.4.
- For more information on this file, see
- https://docs.djangoproject.com/en/2.0/topics/settings/
- For the full list of settings and their values, see
- https://docs.djangoproject.com/en/2.0/ref/settings/
- '''
- import os
- # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
In the above part of your settings.py, BASE_DIR is pointing to the absolute path of the parent directory.
- # Quick-start development settings - unsuitable for production
- # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
- # SECURITY WARNING: keep the secret key used in production secret!
- SECRET_KEY = //long string of some characters
The above part specifies the security key for your project. Django provides you a cryptographic signing API to any app for cryptographically-secure signatures on vales and this security-key is used to makes hashes. It is also used to manage user session, password reset requests, messages, CookieStorage or FallbackStorage. It is important to keep this key secret or attackers could use it to generate their own signed values.
- # SECURITY WARNING: don't run with debug turned on in production!
- DEBUG = True
By default, Django leaves the DEBUG = True. If your app raises an exception when DEBUG is True, Django will display a detailed traceback, including a lot of metadata about your environment, such as all the currently defined Django settings (from settings.py). But here’s a catch. NEVER DEPLOY A SITE INTO PRODUCTION WITH DEBUG TURNED ON. It’s a gaping hole for security, plus it will rapidly consume memory on a production server.
- ALLOWED_HOSTS = []
When DEBUG = False, Django doesn’t work at all unless you populate ALLOWED_HOSTS with a suitable value. This setting is required to prevent an attacker from poisoning caches and password reset emails with links to malicious hosts by submitting requests with a fake HTTP Host header. So Django will only allow access to the hosts that are allowed in this section. If you’re working with tutorials or just building with DEBUG=False, you can specify it to the localhost and everything will work fine.
- ALLOWED_HOSTS = ['localhost', '127.0.0.1']
- # Application definition
- INSTALLED_APPS = [
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- ]
This section lists all the applications that are enabled in this Django installation. Next, whenever we’ll create an app for our project, we would have to add it to this list to let the Django know and allow our app to run.

Join the conversation! Your thoughts help the community grow.