Python is a beautiful language. It’s easy to learn and fun, and its syntax (the rules) is clear and concise. Python is a popular choice for beginners, yet still powerful enough to back some of the world’s most popular products and applications from companies like NASA, Google, IBM, Cisco, Microsoft, Industrial Light & Magic among others.
One area where Python shines is web development. Python offers many frameworks from which to choose from including bottle.py, Flask, CherryPy, Pyramid, Django and web2py. These frameworks have been used to power some of the world’s most popular sites such as Spotify, Mozilla, Reddit, the Washington Post and Yelp. The tutorials and articles in this section cover techniques used in the development of Python Web applications and focus on how to program real-world solutions to problems that ordinary people actually want to solve.
The Web has grown a mindboggling amount in the number of sites, users and implementation capabilities since the first website went live in 1989. Web development is the concept that encompasses all the activities involved with websites and web applications.
Python can be used to build server-side web applications. While a web framework is not required to build web apps, it's rare that developers would not use existing open source libraries to speed up their progress in getting their application working.
Python is not used in a web browser. The language executed in browsers such as Chrome, Firefox and Internet Explorer is JavaScript. Projects such as pyjs can compile from Python to JavaScript. However, most Python developers write their web applications using a combination of Python and JavaScript. Python is executed on the server side while JavaScript is downloaded to the client and run by the web browser.
To become an experienced web developer you need to know the foundation principles that the web is built with, such as HTTP requests and responses, client (typically web browsers) and server (web servers such as Nginx and Apache architectures, HTML, CSS and JavaScript, among many other topics. The following resources provide a range of perspectives and when combined together should get you oriented in the web development world.
How the Internet works is a must-read to get a quick overview of all the pieces that go into a network connection from one machine to another. The example explains how an email is sent and the story is just as useful for learning about other connections such as downloading a webpage.
If you want to be a web developer it's important to know the foundational tools used to build websites and web applications. It is also important to understand that the core concepts such as HTTP, URLs and HTML were all there at the beginning and then were expanded with new specifications over time. This article on the History of the Web succinctly explains the origins of the web starting from Tim Berners-Lee's origin vision and release at CERN.
Web Architecture 101 is a great high-level overview of the technologies that run the modern web, such as DNS, load balancers, web application servers (for Python that equates to WSGI servers), data bases, task queues, caching and several other critical concepts.
What happens when? is an incredibly detailed answer to the questions "What happens when you type google.com into your browser's address box and press enter?" that seems straightforward on the surface until you really dig in.
How browsers work provides an overview with solid detail on how browsers take the HTML, CSS, JavaScript, images and other files as input and render webpages as output. It is well worth your time to know this stuff as a web developer.
The history of the URL explains how the growth of ARPANET to hundreds of nodes eventually led to the creation of the URL. This is a great read that provides historical context for why things are the way they are with the web.
The Browser Hacker's Guide to Instantly Loading Everything is a spectacular technical talk given by Addy Osmani at JSConf EU 2017 that has great bits of developer knowledge for both beginner and experienced web developers alike.
Build a web application from scratch and its follow on posts for request handling middleware explores the fundamentals of web development. Learning these foundational concepts is critical for a web developer even though you should still plan to use an established web framework such as Django or Flask to build real-world applications. The open source code for these posts is available on GitHub.
While not Python-specific, Mozilla put together a Learning the Web tutorial for beginners and intermediate web users who want to build websites. It's worth a look for general web development learning.
Web development involves HTTP communication between the server, hosting a website or web application, and the client, a web browser. Knowing how web browsers works is important as a developer, so take a look at this article on what's in a web browser.
Ping at the speed of light dives into the computer networking weeds with how fast packets travel through the internet plumbing. The author created a Python script that scrapes network speeds from disparate locations to see what the network speed is in fiber optic cables as a percentage of the speed of light.
The critical path: optimizing load times with the Chrome DevTools provides a well-written explanation about using Chrome's developer features to improve the performance of your websites and web applications.
Three takeaways for web developers after two weeks of painfully slow Internet is a must-read for every web developer. Not everyone has fast Internet service, whether because they are in a remote part of the world or they're just in a subway tunnel. Optimizing sites so they work in those situations is important for keeping your users happy.
The History of the URL: Path, Fragment, Query, and Auth gives a comprenhensive historical perspective on the fundamental way to link to resources on the web. This post should be required reading for web developers.
Quantum Up Close: What is a browser engine? explains how a browser takes in HTML, JavaScript, CSS, images and any other data and files to produce a webpage as output.
How to understand performance tests is an important topic because many websites are slow and bloated. Learning about improving the performance of your site is one of the best ways to become a better web developer. Another great article on website performance is The average web page is 3MB. How much should we care?. The visuals alone tell a compelling story about how large webpage sizes have grown in recent years.
Django is an extremely popular and fully featured server-side web framework, written in Python. This module shows you why Django is one of the most popular web server frameworks, how to set up a development environment, and how to start using it to create your own web applications.
Before starting this module you don't need to have any knowledge of Django. Ideally, you would need to understand what server-side web programming and web frameworks are by reading the topics in our Server-side website programming first steps module.
A general knowledge of programming concepts and Python is recommended, but is not essential to understanding the core concepts.
In this first Django article we answer the question "What is Django?" and give you an overview of what makes this web framework special. We'll outline the main features, including some advanced functionality that we won't have time to cover in detail in this module. We'll also show you some of the main building blocks of a Django application, to give you an idea of what it can do before you set it up and start playing.
Setting up a Django development environment
Now that you know what Django is for, we'll show you how to set up and test a Django development environment on Windows, Linux (Ubuntu), and macOS — whatever common operating system you are using, this article should give you what you need to be able to start developing Django apps.
Django Tutorial: The Local Library website
The first article in our practical tutorial series explains what you'll learn, and provides an overview of the "local library" — an example website we'll be working through and evolving in subsequent articles.
Django Tutorial Part 2: Creating a skeleton website
This article shows how you can create a "skeleton" website project, which you can then go on to populate with site-specific settings, URLs, models, views, and templates.
Django Tutorial Part 3: Using models
This article shows how to define models for the LocalLibrary website — models represent the data structures we want to store our app's data in, and also allow Django to store data in a database for us (and modify it later on). It explains what a model is, how it is declared, and some of the main field types. It also briefly shows a few of the main ways you can access model data.
Django Tutorial Part 4: Django admin site
Now that we've created models for the LocalLibrary website, we'll use the Django Admin site to add some "real" book data. First, we'll show you how to register the models with the admin site, then we'll show you how to login and create some data. At the end, we show some ways in which you can further improve the presentation of the admin site.
Django Tutorial Part 5: Creating our home page
We're now ready to add the code to display our first full page — a home page for the LocalLibrary that shows how many records we have of each model type, and provides sidebar navigation links to our other pages. Along the way we'll gain practical experience in writing basic URL maps and views, getting records from the database, and using templates.
Django Tutorial Part 6: Generic list and detail views
This tutorial extends our LocalLibrary website, adding list and detail pages for books and authors. Here we'll learn about generic class-based views, and show how they can reduce the amount of code you have to write for common use cases. We'll also go into URL handling in greater detail, showing how to perform basic pattern matching.
Django Tutorial Part 7: Sessions framework
This tutorial extends our LocalLibrary website, adding a session-based visit-counter to the home page. This is a relatively simple example, but it does show how you can use the session framework to provide persistent behavior for anonymous users on your own sites.
Django Tutorial Part 8: User authentication and permissions
In this tutorial we'll show you how to allow users to login to your site with their own accounts, and how to control what they can do and see based on whether or not they are logged in and their permissions. As part of this demonstration, we'll extend the LocalLibrary website, adding login and logout pages, and user- and staff-specific pages for viewing books that have been borrowed.
Django Tutorial Part 9: Working with forms
In this tutorial we'll show you how to work with HTML Forms in Django, and in particular the easiest way to write forms to create, update and delete model instances. As part of this demonstration, we'll extend the LocalLibrary website so that librarians can renew books, and create, update, and delete authors using our own forms (rather than using the admin application).
Django Tutorial Part 10: Testing a Django web application
As websites grow they become harder to test manually — not only is there more to test, but, as the interactions between components become more complex, a small change in one area can require many additional tests to verify its impact on other areas. One way to mitigate these problems is to write automated tests, which can easily and reliably be run every time you make a change. This tutorial shows how to automate unit testing of your website using Django's test framework.
Django Tutorial Part 11: Deploying Django to production
Now you've created (and tested) an awesome LocalLibrary website, you're going to want to install it on a public web server so that it can be accessed by library staff and members over the internet. This article provides an overview of how you might go about finding a host to deploy your website, and what you need to do in order to get your site ready for production.
Django web application security
Protecting user data is an essential part of any website design. We previously explained some of the more common security threats in the article Web security — this article provides a practical demonstration of how Django's built-in protections handle such threats.
The following assessment will test your understanding of how to create a website using Django, as described in the guides listed above.
In this assessment you'll use some of the knowledge you've learned from this module to create your own blog.