Django is an open-source, high-level and free Web framework created using Python. It follows the Model View Template(MVT) architectural pattern. In this tutorial you’ll learn to setup a Django 2.2 project from scratch.
Why Django?
Django is one of the best framework available today. It acts as a quick solution for web development and delivers high-quality code with better security. Django takes it very seriously and helps to avoid SQL injection, cross-site scripting etc. Let’s look at some of the core features in Django
- Faster : It’s considered to be one of the fastest framework built by python language.It encourages rapid development with a clean and pragmatic design.
- More Packages : Django comes with many components that helps to develop faster and easier.No need of downloading any components separately as Django installs all the extras, packages and the related dependencies.
- Versatile : Django is used to develop all sorts of applications β from basic development to complex development. Therefore, Django is extremely versatile in all fields.
- Secure : Django is as secure as any web framework can be. It provides tools to prevent common mistakes causing security problems.
Installing Django
#Note
Make sure you have installed Python in your PC. Click onΒ Install PythonΒ If you haven’t installed Python on your PC/ Mac.
Type the following command at your terminal to install Django on your PC/ Mac
pip install Django==2.2
Starting a new Django application
Now that we have completed the basic setup, to start Django application. Let’s start the Project.
django-admin startproject sampleProject
After initiating the project. Type the following command to get into the project directory
cd sampleProject
After creating the project, you will find a list of files inside the project directory as below.
|-- sampleProject | |-- manage.py | |-- sampleProject | | |--_init_.py | | |--settings.py | | |--urls.py | | |--wsgi.py
- _init_.py : Init just tells the python that this is to be treated like a python package.
- settings.py : Manages all the settings of your project.
- urls.py : The main controller which maps it to the website.
- wsgi.py : Serves as an entry point for WSGI compatible web servers.
|-- sampleProject | |-- manage.py | |-- sampleProject | |-- app | | |--migrations | | | |--_init_.py | | |--_init_.py | | |--admin.py | | |--apps.py | | |--models.py | | |--tests.py | | |--views.py
Type the following command at your terminal to create the list of files inside the app as shown above.
python manage.py startapp app
Code language: CSS (css)
Now add the following code to the views.py file which will return a httpResponse
app/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1 st
Code language: JavaScript (javascript)
To map the view with the URL, add the following code in urls.py
sampleProject/urls.py
from django.contrib import admin
from django.urls import path
from app.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('',index),
]
Code language: JavaScript (javascript)
Now It’s time to run the application, To start the server simply type the command show below at your terminal.
python manage.py runserver
Code language: CSS (css)
Feel free to drop your thoughts at the comment section!