Getting Started with Django - Building our first Django App Part -1

| 3 min read | 42

User Avatar

User has not set any headline

Blog Wallpaper

Introduction

Django is a web framework for perfectionists with deadlines. It comes with a built-in authentication and authorization system. Authentication refers to verify if user is valid while authorization implies that a user is allowed to perform certain actions. We can start a Django project and setup authentication within couple of minutes. In this tutorial we will learn to setup user authentication using django's built-in authentication system.

 

Getting started with Virtual Environment

Lets start by creating a virtual environment. In this tutorial, we will be using virtualenv. Install the virtualenv by running:

user@ubuntu:~$ pip install virtualenv

Now, we need to create a virtual environment. To create a virtual environment, we will run the following command:

user@ubuntu:~$ virtualenv env_name

Here env_name is the name of virtual environment. In this case, I am going to use django_demo.

So the command will be virtaualenv django_demo in my case.

Now we will install django to our virtual environment. Before that, we need to activate our virtual environment.

user@ubuntu:~$source django_demo/bin/activate

After running this command, you can verify that the virtual environment has been activated successfully or not. If the prefix has changed in your terminal, then it will indicate that you have successfully activated the virtual environment. For example, after activating the virtual environment, it will look like this:

(django_demo) user@ubuntu:~

There is one more to way to check whether virtual environment was activated successfully or not. In your terminal, run which python, it should print the location of python interpreter of your virtual environment.

 

(django_demo) user@ubuntu:~$ which python
(django_demo) user@ubuntu:~$ django_demo/bin/python

This should be the order for running these commands.

Once we have successfully activated our virtual environment, we can proceed to the next step.


Setting up Django

We will use pip (or pip3 based on your python version) to install django in our virtual environment.

(django_demo) user@ubuntu:~$ pip install django

We can verify successful installation by checking the django version.

(django_demo) user@ubuntu:~$ python -m django --version

Above command should print the version of django. Now, we can create our django project by using django-admin.

We can create a new django project by running:

(django_demo) user@ubuntu:~$ django-admin startproject django_blog

It will start a django project under the directory django_blog. Inside the django_blog directory, there will be manage.py, and another folder with the same name as the project name, it will hold settings.py, wsgi.py and urls.py files.

 

 

manage.py is created automatically whenever we start a new django project. It performs the same actions as django-admin but also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.


Running the Django project

To start the django development web server on the local machine, we will run runserver command.

(django_demo) user@ubuntu:~$ python manage.py runserver

It will start a development server using port 8000 by default. If the command executes without any error and you hit the url http://localhost:8000/ in the browser, you will see the default welcome page of django.

You can read about manage.py and admin.py commands at Django's documentation page.

10

TAGS

COMMENTS

You need to login to comment on this post.