DEV Community

dow2
dow2

Posted on

Introduction to Django

Today I would like to discuss with you one of Pythons most popular framework if not the most popular framework, Django. Django is described as a high level web framework with capabilities that enables the rapid development of secure, scaleable, and maintainable websites. Django follows a model template views architectural plan. Before getting too deep with the ins and outs of the Django framework I would like to give a little pretence and context to the programming language it is built on, Python.

Python is an interpreted general purpose programming language which is dynamically typed and garbage collected. Python was first released in 1991 as Python 0.9.0 and is currently on Python 3.0 which was released in 2008. More specifically we are on version 3.9.12 which was released in 2022. The language was created by Guido Van Rawson in the Netherlands, his original intention was for it to be a successor to the ABC programming language. Python is thought to be a beginner friendly programming language which has gone on to make it one of the most used programming languages today.

Now that you have a general understanding of the programming language that Django was built upon we can dive a little deeper into why the framework was created and the features that make it useful to adopt. Django was created by Simon Willison and Adrian Holovaty. Adrian and Simon were enamored by Python more specifically mod_python however they were not convinced that it would scale so they began developing what would become known as the framework Django with the intention of creating a thin abstraction layer between mod_python and the code that they were developing for their newsroom.

Today I would like to provide a general introduction to the features that have made Django the most popular framework on one of the most popular programming languages. Some key concepts I would like to discuss are Apps, Views, Url Routing, Template Language, Models, Django Orm, Admin Panel, Static Files, Authentication, Django Commands, and Signals.

Apps

At the heart of every Django project is the utilization of Apps, which are small pieces of our project which come together to make our website. Apps represent different parts of our website. Apps would be housed in a main project folder and registered in projects settings file.

Views

  • Python functions that takes http requests and returns http response.
  • Can be written as Class Based or Function Based.
  • Function based views are considered easier for beginners and in most cases the documentation is written using function based

URL Routing

  • To create URL Routing we make a list of url patterns and set different paths to those views.
  • This is how Django knows which view to display when a user visits a certain url Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL
  • Once one of the URL patterns matches, Django imports and calls the given view
  • Use the path function, set a url route and assign an associated view

Template Language

  • Most web frameworks have a template system that allows the developer to separate the presentation layer from the application logic
  • Django mainly functions with a backend so, in order to provide a frontend and provide a layout to our website, we use templates.
  • a convenient way to generate HTML dynamically.

Models

  • Class based representations of our database tables
  • Considered to be the core of how we design our database
  • Class represents a table and each attribute represents a column in the table

Django ORM

  • Django has a built in ORM or Object Relational Mapper for communicating with and manipulating the Database
  • Uses methods like get(), all(), and filter() to interact with the database.

Admin Panel

  • A quick start interface designed to help us work with our data
  • Reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site.
  • Can view, create, and update any data in our database
  • High customizable to fit a particular users needs

Static Files

  • Websites generally need to serve additional files such as images, JavaScript, or CSS.
  • In Django, these files are referred to as “static files”.
  • Django provides django.contrib.staticfiles to help manage these files.
  • Django has practices set in place for serving these files

Authentication

  • Django comes with a built in user authentication system.
  • It handles user accounts, groups, permissions and cookie-based user sessions.
  • Handles both authentication and authorization.
  • Authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do.
  • Makes handling Login, LogOut, Change Password and User Registration easier undertakings

Django Commands

  • Powerful Commands built into the Django framework
  • I will list 5 useful commands every Django Programmer should know
  1. startproject
  2. startapp
  3. makemigrations
  4. migrate
  5. createsuperuser

Django Signals

  • Powerful tool used for listening for events that may occur inside of our Django Application.
  • Uses ‘senders’ and ‘receivers’ to listen for events and fire off actions.
  • Signals allow certain senders to notify a set of receivers that some action has taken place.

Top comments (1)

Collapse
 
sokhavuth profile image
Sokhavuth TIN • Edited

The Django's slogan is "The web framework for perfectionists with deadlines." It means that if you don't have time to build everything by your self, you can use Django framework, it will do almost everything for you. What you need to do is creating data models.

If we come across many web frameworks, we will see that they have almost the same structure: routes->controllers->models->views. I always wonder why we say MVC not RCMV.

Sometime, different frameworks use different terms to call the same thing. For example, Django uses "url" as the term for "route" in other frameworks; Django calls "app" as the term for "blueprint" in Flask, Sanic, and Bottle web frameworks; Django calls "view" for the term "controller" in many other web frameworks; Django calls "template" for the term "view" in other framework.