What is a Web Framework
In order to work on a web development project, a programmer is required to leverage one of the numerous web frameworks to write code that’s dedicated to function as a highly efficient and scalable web server that manages and serves content to end users.
A web framework is a software library that is designed to help the development of any web site. By packaging common web functionalities into itself, a web framework allows programmers to create web sites using out-of-the-box code and infrastructure so that they can focus on building business logic oriented features of the web site instead of writing code that deals with common web programming tasks over and over again. For example, most web frameworks provide libraries for database access, templating engine and web session management. Without a web framework, one would be forced to write the same boiler-plate code to handle these common tasks whenever one starts to work on a new project.
Django is a popular web framework for Python. Although it is packed with lots of features right out of the box, these features may seem disjointed to a person who is not familiar with Django. In this Django series, we are going to write a non-trivial blog website that is cohesive, testable and scalable by leveraging all the power provided by Django and its thriving community.
How do we achieve this goal? Well, we maintain several principles during coding:
- Cohesive: DRY (Don’t Repeat Yourself) means that we write code that does one thing and one thing only. If we write a function, then that function does only one thing, either processing data or computing result, but never doing many things at once.
- Testable: means that we always write test code that accompanies the actual code. If we implement a view, then we will also implement the test code for that view. If we implement a model class, then we also write code that tested the model’s methods and values against a fixture database.
- Readable: means that we always document the code in a way that is clear, concise and comprehensible. Notice that readable code does not mean code packed with excessive comments. We write comments in order to explain context, intention and behaviour of a piece of code instead of simply for the sake of writing comments.
Overall, our goal is to present code written with the aforementioned principles and convince you, the reader, that Django is one of the best web frameworks.
Django vs. Other Python Web Frameworks
Of course, Django is not the only Python web framework out there. Other popular Python web frameworks are:
- Flask: An open source micro web framework that provides only the core libraries of web development. It does not provide database abstraction, form validation or any other component that can be provided a third-party component. Unlike Django, the number of out-of-the-box components provided by Flask is kept to a bare minimum, which is why it’s called a micro web framework.
- Pyramid: An open source minimalistic web framework inspired by Zope, Pylons and Django. It strictly adheres to the MVC (Model-View-Controller) pattern and persistence agnostic. Compared to Django, Pyramid provides an extra URL mapping mechanism called URL traversal which maps URLs to views through a predefined dictionary data structure.
- web2py: An open source macro web framework inspired by Ruby-on-Rails. It follows Ruby-on-Rails’ convention over configuration approach and provides sensible defaults for various components and libraries. Compared to Django, web2py is different because it handles almost everything for a programmer while providing fewer configuration options.
Notice that there’s no ranking between these frameworks. No matter which web framework you choose, at the end of the day the most important question is answered by yourself or your team: Do I enjoy using this framework? An enjoyable-to-use web framework brings out the highest efficiency of any programmer. Therefore, choose a framework you love and use it to create elegant web sites.
Websites Powered by Django
Here’s a non-exclusive list of web sites powered by Django:
- pinterest.com: Pinboard-styled photo sharing site.
- bitbucket.org: Code sharing site.
- instagram.com: Photo sharing site.
- addons.mozilla.com: Firefox browser’s addon management site.
- disqus.com: Blog-comment hosting site for communities.
As you can see, Django is more than capable to host some of the most popular social sharing websites such as Pinterest and Instagram as well as some of the most mission critical websites such as Bitbucket. Due to the best software practices promoted and maintained by Django, big companies are also starting to adopt the new technology and slowly transitioning out of J2EE.
Thinking about URL Design
Before we dive into the details about implementing a Django-powered blog website, let’s talk about how to design our website’s urls in a sensible way. Notice that we are not going to dive into the details of Django’s URL mappings, instead we are going to design the URLs without any regard to the underlying mechanism.
We want to create a blog, which should have several top-level components, such as user management, social network management, post management and multi-media management. Let’s inspect each component in more details.
The user management component should deal with common tasks such as sign up, login, logout and profile creation and edition. The social network management component should handle users following other users, private / public posts and comments and third party open auth frameworks such as Facebook and Google+. The post management component is in charge of post creation, editing, deletion and search. The last component, multi-media management, should handle storage of photos and videos on a backend system such as server-side disks or Amazon S3.
Based on the DRY principle, we should expose a list of URLs or endpoints for these top-level components, each of which corresponds with a specific task. Therefore, we are going to draft our list of URLs or endpoints in the following way:
- User Management:
- Social Network Management:
- Post Management
- Media Management
Notice that the current design is by no means final. It’s common that we will modify our URL design as we keep implementing the website. So keep in mind that no matter how well designed a software project is from the start, there will always be new features, requirements or changes later. Therefore, instead of sticking to a design and not budging when changes are required, we should be open-minded about the possible changes and write code that is robust and easy to change.
In this opening article, we took a look at Django through a bird’s-eye view and learned the outlines of generic URLs for our website. Since the future articles of this series will gradually implement the URL endpoints one by one, it might be helpful to come back to this article from time to time during our journey of implementing our first Django website.