Book Image

Building Django 2.0 Web Applications

By : Tom Aratyn
Book Image

Building Django 2.0 Web Applications

By: Tom Aratyn

Overview of this book

<p>This project-based guide will give you a sound understanding of Django 2.0 through three full-featured applications. It starts off by building a basic IMDB clone and adding users who can register, vote on their favorite movies, and upload associated pictures. You will learn how to use the votes that your users have cast to build a list of the top 10 movies. This book will also take you through deploying your app into a production environment using Docker containers hosted on the server in Amazon's Electric Computing Cloud (EC2). </p><p> </p><p>Next, you're going to build a Stack Overflow clone wherein registered users can ask and answer questions. You will learn how to enable a user asking a question to accept answers and mark them as useful. You will also learn how to add search functionality to help users find questions by using ElasticSearch. You'll discover ways to apply the principles of 12 factor apps while deploying Django on the most popular web server, Apache, with mod_wsgi. Lastly, you'll build a clone of MailChimp so users can send and create emails, and deploy it using AWS. </p><p> </p><p>Get set to take your basic Python skills to the next level with this comprehensive guide! </p><p></p>
Table of Contents (19 chapters)
Title Page
www.packtpub.com
Contributors
Preface
Index

Preface

Who doesn't have an idea for the next great app or service they want to launch? However, most apps, services, and websites ultimately rely on a server being able to accept requests and then create, read, update, and delete records based on those requests. Django makes it easy to build and launch websites, services, and backends for your great idea. However, despite the history of being used at large-scale successful start-ups and enterprises, it can be difficult to gather all the resources necessary to actually take an idea from empty directory to running production server.

Over the course of three projects, Building Django Web Applications guides you from an empty directory to creating full-fledged apps to replicate the core functionality of some of the web's most popular web apps. In Part 1, you'll make your own online movie database. In Part 2, you'll make a website letting users ask and answer questions. In Part 3, you'll make a web app to manage mailing lists and send emails. All three projects culminate in your deploying the project to a server so that you can see your ideas come to life. Between starting each project and deploying it, we'll cover important practical concepts such as how to build APIs, secure your project, add search using Elasticsearch, use caching, and offload tasks to worker process to help your project scales.

Building Django Web Applications is for developers who already know some basics of Python, but want to take their skills to the next level. Basic understanding of HTML and CSS is also recommended, as these languages will be mentioned but are not the focus of the book.

After reading this book, you'll be familiar with everything it takes to launch an amazing web app using Django.

Who this book is for

This book is for developers who are familiar with Python. Readers should know how to run commands in Bash shell. Some basic HTML and CSS knowledge is assumed. Finally, the reader should be able to connect to a PostgreSQL database on their own.

What this book covers

Chapter 1, Building MyMDB, covers starting a Django project and the core MyMDB Django app. You will create the core models, views, and templates. You will create URLConfs to help Django route requests to your views. By the end of this chapter, you will have a tested Django project that you can access using your web browser.

Chapter 2, Adding Users to MyMDB, covers adding user registration and authentication. With users being able to register, log in, and log out, you will accept and display votes on movies. Finally, you'll write aggregate queries using Django's QuerySet API to score each movie.

Chapter 3, Posters, Headshots, and Security, covers securely accepting and storing files from your users. You'll learn about the top web application security issues, as listed in the OWASP Top Ten, and how Django mitigates those issues.

Chapter 4, Caching in on the Top 10, covers how to optimize Django projects. You'll learn how to measure what needs optimization. Finally, you'll learn about the different caching strategies Django makes available and when to use them.

Chapter 5, Deploying with Docker, covers how to deploy Django using Nginx and uWSGI in a Docker container. You'll learn how to store uploaded files in S3 to protect the user. Finally, you'll run your Docker container on a Linux server in the Amazon Web Services cloud.

Chapter 6, Starting Answerly, covers creating the models, views, templates, and apps for the Answerly project. You'll learn how to use Django's built-in date views to show a list of questions asked each day. You'll also learn how to split large templates into more manageable components.

Chapter 7, Searching for Questions with Elasticsearch, covers working with Elasticsearch to let users search our questions. You will learn how to create a service that avoid coupling external services to your models or views. You will also learn how to automatically load and update model data in an external service.

Chapter 8, Testing Answerly, covers how to test a Django project. You will learn how to measure code coverage in a Django project and how to easily generate test data. You will also learn how to write different types of tests from unit tests to live server tests with a working browser.

Chapter 9, Deploying Answerly, covers how to deploy a Django project on a Linux server with Apache and mod_wsgi. You'll also learn how to treat your Django project like a twelve-factor app to keep it easy to scale.

Chapter 10, Starting Mail Ape, covers creating the models, views, templates, and apps for the Mail Ape project. You'll learn how to use alternate fields for non-sequential primary keys.

Chapter 11, Sending Emails, covers how to use Django's email functionality. You'll also learn how to use Celery to process tasks outside of the Django request/response cycle and how to test code that relies on Celery tasks.

Chapter 12, Building an API, covers how to create an API using the Django REST Framework (DRF). You'll learn how DRF lets you quickly build an API from your Django models without repeating a lot of unnecessary code. You will also learn how to access and test your API.

Chapter 13, Deploying Mail Ape, covers how to deploy a Django app into the Amazon Web Services cloud. You'll learn how to make an Amazon Machine Image (AMI) a part of a release. Then, you'll create a CloudFormation template to declare your infrastructure and servers as code. You’ll take a look at how to use AWS to horizontally scale your system to run multiple web workers. Finally, you'll bring it all online using the AWS command-line interface.

To get the most out of this book

To get the most out of this book book you should:

  1. Have some familiarity with Python and have Python3.6+ installed
  2. Be able to install Docker or other new software on your computer
  3. Know how to connect to a Postgres server from your computer
  4. Have access to a Bash shell

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Building-Django-2.0-Web-Applications. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

 

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "It also offers a create() method for creating and saving an instance."

A block of code is set as follows:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

DATABASES = {
  'default': {
     'ENGINE': 'django.db.backends.sqlite3',
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  }
}

Any command-line input or output is written as follows:

$ pip install -r requirements.dev.txt

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Clicking on MOVIES will show us a list of movies."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.