Django – A Python Web Framework for Perfectionists with Deadlines
Django is a free and open-source collection of libraries written in Python, which follows the model-view-template-controller (MVTC) architectural pattern. It is one of the most popular server-side web frameworks with the motto being ‘Don’t Repeat Yourself’. It is maintained by the Django Software Foundation (DSF), an independent non-profit organization.
Django’s primary goal is to ease the creation of complex, database-driven websites emphasizing reusability and “pluggability” of components, less code, low coupling, rapid development, and the principle of don’t repeat yourself. One of the cool features of Django is, it provides an optional administrative dashboard for performing create, read, update and delete (CRUD) operations that are generated dynamically based on the admin models and the user-written models.
History of DjangoDjango was created in the fall of 2003, when the web programmers at the Lawrence Journal-World newspaper, Adrian Holovaty and Simon Willison, began using Python to build applications. It was released publicly under a BSD license in July 2005. The framework was named after guitarist Django Reinhardt. In June 2008, it was announced that a newly formed Django Software Foundation (DSF) would maintain Django in the future.
Key Features of Django
- Modularity is the key selling point. Features of django are decoupled so that developers can pick and choose the module of interested right for the project
- A free rich API – Automatically generates Python API based on models
- Database creation automatic – Using the migrate and makemigrations command, django will generate the database tables required using the class models specified
- Admin dashboard – a quick and easy way to populate the data
- RSS / Atom Feed framework – quickly create the feeds from the python classes
- Powerful cache framework for dynamic websites
- Powerful built-in template system
- Easy database migrations
- Security features – protect against XSS, CSRF attacks, SQL injections
Design Pattern used in Django – Model-View-Template-Controller Architectural Pattern
The model is a model or the representation of your data. It’s not the actual data, but an interface to the data. The model allows you to pull data from your database without knowing the intricacies of the underlying database. The model usually also provides an abstraction layer with your database, so that you can use the same model with multiple databases.
The presentation layer. The template is what you see. This layer contains presentation-related decisions: how something should be displayed on a Web page or other type of document.
The business logic layer. This layer contains the logic that accesses the model and defers to the appropriate template(s). You can think of it as the bridge between models and templates.
The URL Dispatcher maps the requested URL to a view function and calls it.
Installing DjangoThis is a simple 3 step process
- Ensure you have Python installed on your system, else visit here for python installation. https://www.python.org/downloads/
- pip3 install virtualenv
- mkdir djangoblog
- virtualenv djangoblog
- cd djangoblog
- source djangoblog/bin/activate
3) Installing Django
pip3 install Django
The current version Django 2.1, will be installed in your virtual environment system.
Experiment with a Django Project:Let us walk through how a Django framework can be used to build a simple full stack application. Here we are taking an example of building a blogger app using django.
A project is a collection of settings and some auto-generated files and folders for an instance of Django project. The auto-generated code contains
- a collection of settings for an instance of Django
- database configuration
- Django-specific options and application-specific settings.
django-admin startproject myblogger
The project folder has been created in the folder, where I have run the startproject command
Now all the associated python files and folders will be created in the project folder. Now you are set to work on the Django project.
Folders and Files of the Django Project
- It puts your project’s package on sys.path.
- It sets the DJANGOSETTINGSMODULE environment variable so that it points to your project’s settings.py file.
Some of the subcommands that will be useful for our project are:
makemigrations – makemigration will create the migration. A migration basically tells your database how it’s being changed(i.e new column added, new table, dropped tables etc.)
migrate – migrate is what pushes your changes to your database. It will run all the migrations created(or the ones that haven’t been pushed yet).
runserver – Starts a lightweight development Web server on the local machine. By default, the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an IP address and port number explicitly.
This file contains the database configuration setting, mail setting, language used, and all other configurations for your project. Once you have started your project, this file has to be edited first. You can change the database engine, you use, for the project and the database name. In this case, we are using sqlite3 database and database name is stories.db. The other settings details you might want to change will be timezone, language.
The INSTALLED_APPS, initially contains all the apps that have been installed in your project by Django. When you create an app, you will have to add the name of the app, here. MIDDLEWARE is a low-level plugin system, those plugins that will be essential for your project, like Google Authenticator, will be put up in Middleware. The important thing to be noted here is the order in which the plugins are kept. When you add a middleware, be careful to add it according to the sequence in which your application runs.
Running the projectpython3 manage.py runserver
If your screen is like this after running the runsever subcommand with manage.py, then your Django installation and the initial setup has been completed and the django server is running at localhost.
Enter web browser URL (Eg: Mozilla Firefox), the server address given: http://127.0.0.1:8000/
Now I see that you are happy in seeing, a little colour in your application. This is the web page, showing that your Django application has been installed successfully, and you can begin working on your project.
And about the red commands, that you saw in the terminal can be removed, by following the below steps:
- To shutdown the server, Press Ctrl+C in your terminal
- python3 manage.py makemigrations
- python3 manage.py migrate
python3 manage.py runserver
There will be no red alert errors, just the clean terminal saying that your project is running.