Running your first Django app

Running your first Django app

Django is a python open-source framework used for build web applications. It is known for its simplicity, flexibility and scalability. Flask is also a framework built with python. There has been a lot of arguments amongst developers concerning which is a better framework. We would look at their peculiarities later in this article. Now, let's get started with running your first Django app. We are assuming you have pip installed on your system.

To get started, let's prepare our virtual environment

$ py -m pip install virtualenv

Now we will create an isolated virtual environment:

$ virtualenv my_env

We will install Django, if you already have Django installed, skip to the next step:

$ py -m pip install Django

To check if you have Django installed and the version, run:

$ py -m django --version

If Django is installed you should get the version, if it is not installed you will have an error message. Go back to Step 1 and install Django.

Now, we have Django installed and running. We will create a directory for your project. You are to cd into directory you want to start your project. For example, you are at Desktop, create a folder called Project (or any name you like, we are using project)

$ C:\Users\(Your User Name)\Desktop>cd Projects
$ C:\Users\(Your User Name)\Desktop\Projects>

Now, let's go to creating a project

$ C:\Users\(Your User Name)\Desktop\Projects>django-admin startproject mysite

You should see a mysite folder in the directory. Let's go further to see what it is contained in the folder.

hashnode.png

We have the main mysite folder and a manage.py file (a file that helps you interact with django)

Navigate to the root mysite folder

$ C:\Users\(Your User Name)\Desktop\Projects>cd mysite

Once in the mysite folder, run the following commands

$ C:\Users\(Your User Name)\Desktop\Project\mysite>py manage.py migrate

You should see an output like this:

Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying sessions.0001_initial... OK

We then start our development server by running the following command from our root folder (the first mysite)

$ py manage.py runserver

Congrats you have just created your first Django project. We shall be creating your first Django application in the next tutorial. As an individual I prefer working with Django to working with flask. Here is why:

  • Django is a full-stack web framework, whereas Flask is a micro and lightweight web framework.

  • The features provided by Django help developers to build large and complex web applications. On the other hand, Flask accelerates development of simple web applications by providing the required functionality