Introduction to Django
Tags: django, framework, python
Published: 2020-02-18 06:11:43

---
title: Introduction to Django
published: true
description: Django is a web framework, which helps to build web-based apps. This behave gets more moldability to the programmer.
tags: Django, Framework, Python, 
---
<h2>Introduction to Django</h2>
Django is a web-framework, developed in Python. It is free, open-source and used by millions of developers every year. <a href="https://www.nexsoftsys.com/technologies/python-development-services.html">Python Django development is very robust</a> and can handle heavy websites easily. Some of the websites using Django are - Instagram, Pinterest, Bitbucket, etc.

<h2>Why Django</h2>

1. <strong>Extremely Fast</strong> - It was designed to help developers take applications from the ideation phase to the pilot project as quickly as possible.
2. <strong>Secure</strong> - Django takes security on priority and is designed to avoid concerns that developers generally miss.
3. <strong>Scalable</strong> - Few of the busiest sites on the internet are built on Django like - Disqus, Instagram, NASA, Spotify, Pinterest, etc.
4. <strong>All in One</strong> - It contains many extra features that can handle common Web-development tasks. It comes loaded with User authentication, RSS feed, sitemaps, etc.
5. <strong>Versatile</strong> - We can build many different types of web-application using Django like - Content Management Systems, Social networking, just-browsing websites, etc.

Now, let’s start with setting up Django in our system.

1. <strong>Installing Django</strong>
Considering, Python 3.x.x and pip are already installed, run the following command to install Django.
<pre class="highlight python"><code>pip install django</code></pre>
To check if version and if Django is properly installed, run the following command -
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ivmk8ix3s14a3xg2k0uq.jpg)

If any error is logged, check if Django is properly installed.

2. <strong>Creating a Virtual environment and starting the first app</strong>
To keep our code and required libraries isolated, we will create a virtual environment. I have created an environment named venv.
Run the following command to install the virtual environment.

<pre class="highlight python"><code>sudo apt-get install virtualenv <span class="nt">-y</span></code></pre>

Once it is installed, let’s create a virtual environment named venv.

<pre class="highlight python"><code>virtualenv venv</code></pre>

After this, let’s start with the Django project.

We will run the following command to set up skeleton for django -

<pre class="highlight python"><code>django-admin startproject mysite</code></pre>

Here, mysite is the name of my project. You will see that there is a directory created by the name of your project. Go into the directory and you must see a file named manage.py. This file will serve as the entry point to our server and all the commands will be sent through it.

Now, let’s check if the files are working correctly. Run the following command -

<pre class="highlight python"><code>python manage.py run server</code></pre>

Your terminal will show that the server has started running your localhost on port number 8000.
 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7qp3xwkms25o5nfqccj9.jpg)
Ignore the warning in red color for now and let’s check if the server actually started or not.  Open your browser and open the URL - 127.0.0.1:8000.

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4catw955uarl6nq50b8i.jpg)

Hurray! We just started a Django server.

3. <strong>Setting up a database and playing with migration</strong>

Now, in the previous command that we run, we saw the warning in red color regarding the migrations.
Just run the following command and see the magic -

<pre class="highlight python"><code>python manage.py makemigrations</code></pre>

The terminal shows - “No changes detected”.
This means that there were no changes detected from the previous save. So let’s run the following command to send all the saved changes to the database.

<pre class="highlight python"><code>python manage.py migrate</code></pre>

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/39489mhhcx4fanc5d9uo.jpg)

You must have got something similar to the above picture on your terminal. This means that your database just got set up and you must now see a file named db.sqlite3 in the folder.

4. <strong>Create an app in a project</strong>

Well, our project is now set and we can create apps within our project. I will create an app named demo for demo purposes.

<pre class="highlight python"><code>python manage.py startapp demo</code></pre>

We will now be able to see a directory named demo in our folder.
Open the file named views.py in the demo folder and let’s add the following code to it.
<pre class="highlight python"><code>
from django.http import HttpResponse

def index(request):
    return HttpResponse<span class="s">("Hello, world. Your app is now ready.")</span>
</code></pre>
Now, as we have written a function, we need to map it to a URL to run it. So, we will now create a file named urls.py in the demo directory and write the following code there.
<pre class="highlight python"><code>
<span class="kn">from</span> django.urls import path
from . import views

urlpatterns = [
   path('', views.index, name='index'),
]
</code></pre>
Now, our project does not know that some URL is defined for our app, so we need to create a URL pattern in our project directory as well. Copy-paste the following code in the urls.py file in the mysite directory.
<pre class="highlight python"><code>
from django.contrib import admin
from django.urls import include path

urlpatterns = [
    path('demo/', include('demo.urls')),
    path('admin/', admin.site.urls),
]
</code></pre>
Now run the server to check if the app is running.

Open the following URL - 

http://localhost:8000/demo
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3lptakyuua4khvonl3zw.jpg)
 
Voila! We just created a small Django app to start building a new project.

Read more -
<a href="https://dev.to/divyeshaegis/how-to-set-up-a-rest-service-or-a-web-application-in-django-33gp">How to set up a REST Service or a Web Application in Django</a>