Professional Documents
Culture Documents
Django 1 What Is Django
Django 1 What Is Django
Hello Devs, Welcome to the ocean of Web Development with Django. This blog is packed with
all the information you need to know before you start surfing on this ocean. We will cover the
following topics:
What Is Django?
Working Architecture of Django.
What is PIP?
What is Virtual Environment
Creating, Activating & Deactivating Virtual Environment
Starting Django project
Creating an App
Making Migrations
Creating a SuperUser
The Development Server
What is Django?
Django is an open-source and high-level web application framework written in Python. Django is
specially designed for rapid and clean development. A web framework is a package of
components that helps developers to develop websites faster and easier without handling low-
level details. The framework is named after Django Reinhard, a gypsy jazz guitarist. He has been
called one of the best guitarists of all time. Django follows the “Model View Template” (MVT)
architecture which is slightly different from the (MVC) “Model Controller”.
Model: is a structure for storing data. The model handles everything related to data. Such as:
data access, data validation, dealing with data of other models, etc. It is implemented as a python
class that creates table in our database, and the attributes of the table are created by the fields of
the class. Models are defined inside an app’s ‘models.py’ file.
View: View is the bridge between a model and a template. All the logic to process data of a
model and the data to be sent to a template are written in views. It can be implemented using
python function or Class which receives web request and returns web response. Operations like
Create, Retrieve, Update, Delete are handled in view.
Template: Templates are HTML pages with some special syntax describing how dynamic
content will be inserted. How the data will be presented on a web page is defined in the template
layer. The template layer is basically the presentation layer.
Source: http://www.syntaxbook.com/cover/210-django-mvt
What is PIP?
'pip' is a tool, actually a standard package manager for python used for installing,
uninstalling and managing python packages.
Virtual Environment
A virtual environment is an isolated place where all the necessary packages are
installed for an individual project. Assume that you have multiple projects in your
PC. One of them needs a package named “Pillow” but others don’t. So, you don’t
need to install it globally. Here, you need a virtual environment for each of your
projects where the packages needed only for that specific project will be installed.
Creating Virtual Environment
There are one package and one library to create virtual environment. One is
'virtualenv' & other is 'venv'. 'venv' is a package that can be used only with python-
3. 'virtualenv' is a library which is easier to use and has more functionality.
If you use 'virtualenv' library then the command to create a virtual environment
will be:
virtualenv name-env
On Windows
python -m venv name-env
On Windows
name-env/Scripts/activate
On Unix or Mac OS
source name-env/bin/activate
deactivate
Make sure you give a dot (.) after your project name. It will avoid creating a nested folder with
the same name of your project which can be a thing of confusion and irritation in working time.
After the command is executed you will see this directory tree in your working directory...
[projectname]/
├─├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
Creating an App
We use different functionality in a website. For each functionality, an app can be created as a
fully independent module. The benefits of using apps for different functionality is they are
reusable and loosely-coupled. To create an app in your project you need to go to directory
containing manage.py and from there enter the command :
app_name/
├─migrations/
├───├── __init__.py
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
└── views.py
After creating an app you will have to register it in the list named "INSTALLED_APPS" lies inside
project/settings.py file.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name',
]
Making Migrations
To convert the Django Models into Database Table Django uses an Object-
Relational-Mapper (ORM). When we do any change in our model, Django tracks
the changes and creates database migration scripts (inside /app /migrations/) to
migrate the changes in the database to match the model.
We run the following commands to define tables for those models in the database.
python manage.py makemigrations
python manage.py migrate
Creating a SuperUser
Every website has one or more superuser/s. SuperUser is just an object of 'User'
model with all the permissions and full control over full admin panel of a website.
If you're logged in as a superuser, you will have access to create, edit, and delete
any object of any model. In django we use the following command to create a
super user.