What is Django?

Django is software you can use to develop web applications quickly and efficiently. Most web applications have several common functions, like authentication, information retrieval from a database, and cookie management. Developers have to code similar functionality into every web app they write. Django makes their job easier by grouping the different functions into a large collection of reusable modules, called a web application framework. Developers use the Django web framework to organize and write their code more efficiently and significantly reduce web development time.

Why do web developers choose Django?

There are several web frameworks on the market. Django was written in the Python language, and it is one among many Python web frameworks. However, developers often prefer the Django web framework over others for the following reasons.

Development speed

The Django framework is well organized and straightforward to install and learn, so you can get started within hours. Django designers created the framework to quickly implement any web architecture in code. It supports rapid development and clean, pragmatic design. You can write code in just a few lines because Django provides a ready-to-use structure for several common web development tasks, such as:

  • User authentication
  • Content administration
  • Site maps
  • RSS feeds

Cost effective

Django is a free and open-source Python project with an active community that reviews and maintains the software. A not-for-profit organization called the Django Software Foundation promotes and supports the use and maintenance of Django. It runs regular meetups, gatherings, and community events that encourage other developers to review and contribute to the Django project. The result is a high-quality, feature-rich web framework that is free to use.

Popular

Thousands of open-source projects and high-profile sites use Django, such as:

  • Instagram
  • Mozilla Firefox
  • Pinterest
  • National Geographic

Because of its popularity, the framework continues to evolve and has a strong support infrastructure. A large number of individuals and companies provide free and paid support for any development challenges you might face when you're using Django.

How does Django work?

Any web application is made of two parts: server code and client code. The client or website visitor has a browser. When they type a URL into their browser, they send a request to the web server machine on which the web app is running. The server processes the request by using a database and sends information back to the client as a response. The client code displays the information to the visitor as a web page.

Django manages the code for this request and response system by using a Model-View-Template (MVT) architecture.

Model

Django models act as the interface between the database and the server code. They are the single definitive source of information about your data. These data models contain the essential fields and operations you require to interact with your database. Django models thus convert your database tables into classes or objects in the Python code. This is called object-relational mapping.

Generally, each model maps to a single database table and has attributes that represent the database fields. If, for instance, your website comprises employee details it could be represented as:

  • An employee table with employee names and address fields.
  • An employee model called Class Employee with two attributes, or model fields, called Name and Address.

View

Django views process the request by using the models. You can write a view function for each type of request that your website visitors can make to your website. A view function can take the request as input and return a response, which can be an error code, an image, a file, or any type of data.

Django has a URL mapper, or URL dispatcher, feature that maps your view functions to your URLs. You have to create a URL mapper file in which you write URL patterns as shown below.

urlpatterns = [

path('employee/name', views.employee_name),

path('employee/<int:year>/', views.year_archive),

]

For example, if you want your website visitors to view a list of all your employees in a specific year, then you set up the URL path employee/year number, and you write a corresponding Django view function year_archive. When your website visitor types "yourwebsitename.com/employee/2020" into their browser, the following steps occur:

  1. The request comes to your web app.
  2. The Django web framework takes the year number and view function name from the URL mapper.
  3. It runs the view function year_archive for the year 2020.
  4. Year_archive uses the employee model to get all the employee data from the database for 2020.
  5. The Django web framework sends the data back as a response.

Template

Django templates manage the web page presentation in the browser. Since most web pages are in the Hypertext Markup Language (HTML), you can write Django template code in a style similar to HTML. A template file contains certain components:

  • The static parts of the final HTML output, such as images, buttons, and headers.
  • Special syntax describing how to insert dynamic content or data, which changes with every request.

The following components make up the Django template system.

The template language

The template language is the programming language you use to write the HTML template code. Django supports its own Django Template Language and a popular alternative called Jinja2.

The template engine

The template engine processes the template file and creates the final HTML output. It includes the data from the response in this output.

For example, when your website visitor requests employee information, your Django template will populate the web page that they see with your website header, a table that contains the names and addresses of all employees, and a button that says Next.

What other modules can you use in Django?

Although the Model-View-Template (MVT) architecture defines the basic structure of any application, Django has several other modules to enhance your website. We give some examples below.

Forms

Most websites require forms, for tasks like registration and payment or to collect information from the website visitor. Django provides many tools and libraries that you can use to manage your website forms. It can simplify and automate form processing and can do so more securely than if you write the code yourself.

Django handles form processing in three ways:

  1. Form creation by preparing and restructuring data for display
  2. Form validation by checking HTML forms on the client side
  3. Form processing by receiving the submitted data

User authentication

Contemporary websites have to authenticate and authorize users. Authentication verifies a user's identity, and authorization decides what an authenticated user can do on the site. Django can manage authentication for various uses.

  • User accounts
  • Permissions and yes-or-no flags that allow users to perform certain website tasks
  • Groups of multiple user accounts with similar permissions
  • Cookie-based user sessions

It also provides a configurable password hashing system and tools to restrict content in forms and views.

Site administration

The Django administration site makes it straightforward to provide an admin page for your site. Site administrators can use the page to create, edit, or view the data models on your site.

Is Django opinionated?

We informally call web frameworks “opinionated” when they enforce a process on web developers. They have an opinion or a right way in which developers have to accomplish certain tasks. For example, opinionated frameworks typically support efficient development in specific industries by keeping detailed documentation for application tasks related to that industry.

On the other hand, unopinionated frameworks have fewer restrictions on how you can integrate the different Model-View-Template (MVT) components together. While they are more flexible, they cause complications in code organization because different developers can use varied approaches to the same task.

Django is somewhat opinionated. It provides a wide range of components and includes documentation about how to handle many different types of web development tasks. Developers can use Django's decoupled architecture to choose from a range of options, and it even adds support for new options they require.

What is Django security?

Cybercriminals often target web apps to access user login, financial data, and other sensitive information. The Django web framework offers several features to protect your web apps and your users. You avoid many common security mistakes by following Django best practices. We give some examples below.

Cross-site scripting protection

Cross-site scripting (XSS) attacks occur when cybercriminals insert malicious code into the browsers of your website users. They can attack your users by tricking your web app in several ways, such as:

  • Storing malicious scripts in your database so the server inadvertently sends malicious code in its next response.
  • Tricking your users into clicking a link that causes malicious client code, instead of your code, to run in the user's browser.

For example, the Django template might have {{name}}, which shows the text a user enters in the name field of their profile. The cybercriminal hijacks the session and changes the name to a code such as <script>alert('hello')</script>. The template changes to {{<script>alert('hello')</script>}}.

This code now runs in your user’s browser so that the text message "Hello" pops up on their screen. While this example is simplistic, you can see how a cybercriminal could insert complex scripts to control your user's computer.

Django protects you from such attacks by automatically escaping or ignoring specific characters like < and >, which indicate a malicious code, from user input.

Unauthorized access protection

Cross-site request forgery (CSRF) attacks occur when hackers steal your users' credentials and send unauthorized requests to your web app. Django has built-in protection against most types of CSRF attacks in the Django CSRF module. It works by sending a secret value to every user at their first login. 

New client requests include the secret value as proof that the client is who they claim to be. Since only the authorized user's browser knows the secret value, Django can automatically reject requests if they come from some other machine that is pretending to be the user. You must activate the Django CSRF module setting for it to work.

SQL injection protection

Cybercriminals use SQL injections to insert SQL code into your database by using HTTP requests such as POST. The malicious code can steal or delete your genuine data. Django solves this problem in the design itself. User-sent data, called parameters, are kept separate from the database query until it reaches the model layer. The Django model can then escape dangerous characters when it is creating the query code.

Protection due to community support

Protection in Django goes beyond its built-in security features. Because Django is open source, many experienced developers use and review Django modules. Thorough testing increases the Django code's reliability and prevents accidental security vulnerabilities from being left behind.

What is Django scalability?

Scalability, in website development, refers to the website's ability to handle multiple client requests at the same time. Django projects are very scalable and can handle thousands of requests. You can scale your Django application in the following ways.

Hardware

The Django team designed the web framework to efficiently use the hardware in your system. With a shared-nothing architecture, Django separates components like the database layer (the models) and the application layer (the views). You can add hardware at any level without affecting the rest of the system. You can add more database servers or application servers into your system, and Django will use these resources efficiently to handle multiple visitors.

Caching

Caching is the process of saving some web page data on the client’s server or on intermediary servers so that your Django app can process requests faster, increasing scale. Django offers a robust caching system with different levels of caching:

  1. You can cache your entire website.
  2. You can cache specific view function output.
  3. You can cache specific content that is time consuming to create.

Django projects also work well with third-party caches. You can write code that gives hints about these caches and tells them which part of your application you want to cache.

How can AWS help support your Django web applications?

AWS Elastic Beanstalk is a user-friendly service for deploying and scaling web applications and services that have been developed with technologies like Django, Python, Java, .NET, PHP, Node.js, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS.

Elastic Beanstalk supports Django developers in the following ways:

  • You upload your Django code and Elastic Beanstalk automatically handles the deployment.
  • Elastic Beanstalk provisions and operates the infrastructure and manages the application stack (platform) for you, so you don't have to spend the time or develop the expertise.
  • Elastic Beanstalk automatically scales your application up and down, based on your application's specific need, by using adjustable Auto Scaling settings.

Get started deploying a Django application to Elastic Beanstalk by creating a free AWS account today.

AWS Django next steps

Check out additional product-related resources
View free offers for Developer Tools services in the cloud 
Sign up for a free account

Instantly get access to the AWS free tier. 

Sign up 
Start building in the console

Get started building in the AWS Management Console.

Sign in