Django

Django

INTRODUCTION:

Django is a Web Application Framework which is used to develop web applications.

Before learning Django Tutorial, you must have knowledge of OOPs and Python Programming language.

Definition:

Django is a web application framework written in Python programming language. It is based on MVT (Model View Template) design pattern.By using Django, we can build web applications in very less time.

History:

Django was design and developed by Lawrence journal world in 2003 and publicly released under BSD license in July 2005. Currently, DSF (Django Software Foundation) maintains its development and release cycle.

Django was released on 21, July 2005. Its current stable version is 2.0.3 which was released on 6 March, 2018.

Popularity:

Django is widely accepted and used by various well-known sites such as:

  • Instagram
  • Mozilla
  • Disqus
  • Pinterest

Features of Django:

  • Secure
  • Scalable
  • Versatile
  • Open Source

1.Secure:

Django takes security seriously and helps developers to avoid many common security mistakes.

2.Scalable:

Django is scalable in nature and has ability to quickly and flexibly switch from small to large scale application project.

3.Versatile:

Django is versatile in nature which allows it to build applications for different-different domains. Now a days, Companies are using Django to build various types of applications like: content management systems, social networks sites or scientific computing platforms etc.

4.Open Source:

Django is an open source web application framework. It is publicly available without cost. It can be downloaded with source code from the public repository. Open source reduces the total cost of the application development.

Django Installation:

To install Django, first visit to django official site (https://www.djangoproject.com) and download django by clicking on the download section.

Or in linux terminal just type-pip3 install django

Find the Version-python3 -m django –version

Sample Version-4.0

Create Django Project:

Before Creating Project Just Create a folder(djangoproject) for your djano project.

Within this Folder you start to create your django project.

$ django-admin startproject projectname 

Example:

$ django-admin startproject sampleproject

After change the directory into your current project directory.

$ cd sampleproject

To see all the files and subfolders of django project, we can use tree command to view the tree structure of the application. This is a utility command, if it is not present, can be downloaded via apt-get install tree command.

$ tree -f

$tree .

A Django project contains the following packages and files. The outer directory is just a container for the application. We can rename it further.

  • manage.py: It is a command-line utility which allows us to interact with the project in various ways and also used to manage an application that we will see later on in this tutorial.
  • A directory (djangpapp) located inside, is the actual application package name. Its name is the Python package name which we’ll need to use to import module inside the application.
  • __init__.py: It is an empty file that tells to the Python that this directory should be considered as a Python package.
  • settings.py: This file is used to configure application settings such as database connection, static files linking etc.
  • urls.py: This file contains the listed URLs of the application. In this file, we can mention the URLs and corresponding actions to perform the task and display the view.
  • wsgi.py: It is an entry-point for WSGI-compatible web servers to serve Django project

Running The Django Project:

Django project has a built-in development server which is used to run application instantly without any external web server. It means we don’t need of Apache or another web server to run the application in development mode.

To run the application,

$ python3 manage.py runserver 

or We can change the port number and run the server

$ python3 manage.py runserver 8082

Now click the http://127.0.0.1:8082/ and select open link to see or copy the link and paste it into the browser.

The application is running successfully.

Creating Django App:

Django application consists of project and app. The difference between a project and app is, a project is a collection of configuration files and apps whereas the app is a web application which is written to perform business logic.

$ python3 manage.py startapp appname  

Example:

$ python3 manage.py startapp sampleapp

To view the files and directories in sampleapp.

$tree sampleapp

Now start to Print the ‘Hello world’ in your browser.

Go to your djangoproject folder-sampleproject-sampleapp-views.py

Views.py

rom django.shortcuts import render 
   # Create your views here. 
from django.http import HttpResponse     
def hello(request):      
return HttpResponse("<h2>Hello, Welcome to Django!</h2>") 

Next–>djangoproject folder-sampleproject-sampleproject-settings.py

Type ->sampleapp(your app name) in INSTALLED_APPS

settings.py

INSTALLED_APPS = [    
'django.contrib.admin', 
   'django.contrib.auth',  
  'django.contrib.contenttypes',   
 'django.contrib.sessions',   
 'django.contrib.messages',   
 'django.contrib.staticfiles',  
  'sampleapp'
]

Next–>Go to urls.py

djangoproject folder-sampleproject-sampleproject-urls.py

Type-from sampleapp import views 

 path(‘hello/’, views.hello), 

urls.py

from django.contrib import admin
from django.urls import path
from sampleapp import views 
urlpatterns = [   
 path('admin/', admin.site.urls),   
 path('hello/', views.hello),
]

Run The Application:

Already you know the run server command

$python3 manage.py runserver 8084

You can change your port number.

Now Click the http://127.0.0.1.8084/ and see the output

Why it’s showing page not found error means we give the url name is ‘hello’. So type 127.0.0.1:8084/hello

That’s It you ran your new app.

Reference:https://youtu.be/fuSadH8fr4U

Design a site like this with WordPress.com
Get started