Introduction to Django

Django classes San Francisco

Django is one of the many concepts any aspiring programmer should be familiar with since it is free, open-source and has free and paid documentation and support. Created by experienced developers, this high-level web framework allows you to rapidly develop secure, easily maintainable websites with Python and aims to reduce common web development issues. Once you learn how it works, you’ll be able to use it to write your app instead of starting from scratch to redo existing work.

History of Django

From 2003 to 2005, a web team responsible for maintaining and creating newspaper websites worked to develop the first iteration of Django. After building numerous sites, the team realized they were reusing common code and design patterns, which eventually evolved into the Django project. The open-source web development framework was web-released in July 2005. 

Since then, Django has consistently grown and improved. September 2008 saw the first milestone release from Django and version 2.0 was re-released in 2017. With each new version, Django has added bug fixes and new functionality, with new features like supporting new databases, caching and template engines.

Software Benefits With Django

When you write software using Django, it’ll be complete, versatile and secure.

Complete

Django gives developers nearly every single thing that they will need right away and works as a single product so you don’t have to worry about tools being incompatible. They also all follow consistent design principles and have updated documentation for Django.

Maintainable

The patterns and principles within the Django code encourage you to create reusable and maintainable code by using the DRY (Don’t Repeat Yourself) principle. Because you’re not dealing with unnecessary duplication, it results in less code. Django also groups related functionalities together into reusable applications and related codes into modules.

Portable

Since Django relies on Python, which runs on numerous platforms, you won’t have to limit yourself to a server platform or operating system. Django is also well-supported by multiple web-hosting providers, many of which give specific documentation and infrastructure thanks to its popularity.

Scalable

Django’s scalability is thanks to its use of component-based shared-nothing architecture, which means that every architectural component is independent and can be changed or replaced as needed. Even major sites like Disqus and Instagram have scaled up with Django.

Secure

Because Django includes website protection, developers who use the framework can avoid some of the more common security mistakes. One example includes offering a secure method to manage accounts and passwords to eliminate common issues like storing passwords directly (as opposed to a password hash) and including session info in the cookies.

Django also protects against certain vulnerabilities by default, including clickjacking, cross-site request forgery, SQL injection and cross-site scripting.

Versatile

Django can be used to create nearly any website you need, including news sites, social networks, wikis and document management systems. Plus, it works with all types of client-side frameworks and can give you content in XML, JSON, HTML, RSS feeds and most other formats. Even the internal versatility of Django is impressive as you can use it for functionality from templating engines to popular databases or extend it for using additional components when necessary.

High-Profile Websites Use Django

With all of the benefits that come with using Django’s framework, many major websites have decided to use it to their advantage. 

Django Balances Opinionated and Unopinionated

It is common for web frameworks to be considered either opinionated or unopinionated. Django strikes a balance between the two.

Opinionated frameworks have opinions regarding the correct way to complete certain tasks. In most cases, opinionated frameworks will support the development of a rapid nature in a specific domain since there is a well-documented, well-understood correct method of completing tasks. At the same time, opinionated frameworks tend to have less flexibility with problem-solving outside the focused domain and usually have fewer options in terms of approaches and components.

Unopinionated frameworks usually have fewer restrictions in terms of which components are used and how they are connected. This allows developers to more easily choose the tools that are best for a particular task, but then they must find the relevant components themselves.

Django is considered somewhat opinionated, so it combines the strengths of each approach. While there are components in Django that handle the majority of web development tasks, there are also one or two ideal methods of using the tools. At the same time, Django has a decoupled architecture that lets you choose from various options or add in others.

What You Can Do With Django

Use Django to complete a range of tasks, including assisting with web applications, simplifying the process of creating, validating and processing forms. It also has user authentication and a robust permission system created to maximize security.

To prevent re-rendering (except in situations when it is necessary), Django also delivers flexible caching that lets you store a portion or all of your rendered page. This simplifies the process of serializing and serving data in the form of JSON or XML and makes Django particularly helpful for web services dedicated to serving data without displaying it, or creating a site that uses the client-side code to render the data.

Whenever you use Django to create an application, it will also include the Django administration site that makes it simple to create an admin page that the site admins can use to view, create and edit data models on the site.

Understanding Django Code

Learning about Django is one thing, but most programmers want their Django boot camp to also include some coding. But what does the code actually look like? Django will group code for each individual step into a separate file, resulting in four main types. The organizational structure is called Model View Template or MVT.

URLs

Django tends to be more maintainable if you write separate view functions for every URL resource. This leads to a URL mapper that redirects HTTP requests so they go to the correct view, and matches digit or string patterns in a URL to turn them into data in a view function.

View

This request handler receives HTTP requests and then delivers HTTP responses. Views access the data required to meet those requests using models, then templates format the response.

Models

Models are Python objects which define an application’s data structure and give you the required mechanisms for querying and managing database records.

Templates

The template refers to text files that define a file’s layout or structure using placeholders instead of content. Views dynamically use an HTML template to create their HTML page and then populate it using data from the model. You can also use templates to define any file type’s structure, even if it isn’t HTML.

Sample Django Code

To start visualizing how Django actually works, it helps to take a look at some examples of code. Each focuses on one of the specific tasks involved in using the framework.

urls.py

The file for the URL mapper is usually urls.py. The following example has the mapper define mapping patterns between various routes and their corresponding view functions. The function is called if the system receives an HTTP request with a URL that matches a pattern.

urlpatterns = [
    path('admin/', admin.site.urls),
    path('book/<int:id>/', views.book_detail, name='book_detail'),
    path('catalog/', include('catalog.urls')),
    re_path(r'^([0-9]+)/$', views.best),
]

In this Django example, the object of url patterns features a list of re_path ( ) and path ( ) functions. With both methods, the first argument is a pattern or route to match. The re_path ( ) function is a flexible pattern matching technique where regular expression is used. With the path ( ) method, angle brackets define portions of the URL to go through the function in the form of named arguments.

Conclusion

If this small sampling of Django examples got your attention, consider attending a data class in San Francisco or another tech hub to learn even more. With a bit more Django training and some practice, you’ll be able to use the framework to create your own web applications.

*Please note, these articles are for educational purposes and the topics covered may not be representative of the curriculum covered in our boot camp. Explore our curriculum to see what you’ll learn in our program.

Get Program Info

The following requires your attention:
Back
Back
Back
Back
Back
Back
Back
Back
Back
0%

Ready to learn more about Berkeley Data Analytics Boot Camp in San Francisco? Contact an admissions advisor at (510) 306-1218.