Book Image

Django Project Blueprints

By : Asad Jibran Ahmed
Book Image

Django Project Blueprints

By: Asad Jibran Ahmed

Overview of this book

Django is a high-level web framework that eases the creation of complex, database-driven websites. It emphasizes on the reusability and pluggability of components, rapid development, and the principle of don't repeat yourself. It lets you build high-performing, elegant web applications quickly. There are several Django tutorials available online, which take as many shortcuts as possible, but leave you wondering how you can adapt them to your own needs. This guide takes the opposite approach by demonstrating how to work around common problems and client requests, without skipping the important details. If you have built a few Django projects and are on the lookout for a guide to get you past the basics and to solve modern development tasks, this is your book. Seven unique projects will take you through the development process from scratch, leaving no stone unturned. In the first two projects, you will learn everything from adding ranking and voting capabilities to your App to building a multiuser blog platform with a unique twist. The third project tackles APIs with Django and walks us through building a Nagios-inspired infrastructure monitoring system. And that is just the start! The other projects deal with customizing the Django admin to create a CMS for your clients, translating your web applications to multiple languages, and using the Elasticsearch search server with Django to create a high performing e-commerce web site. The seventh chapter includes a surprise usage of Django, and we dive deep into the internals of Django to create something exciting! When you're done, you'll have consistent patterns and techniques that you can build on for many projects to come.
Table of Contents (15 chapters)
Django Project Blueprints
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Appendix A. Development Environment Setup Details and Debugging Techniques

This appendix will go into further detail about the Django development environment setup that we have been using throughout the book. We will look into the details of the setup, and I will explain each of the steps that we took. I will also show you a technique for debugging Django applications. For this appendix, we will assume that the project we are setting up is the Blueblog project from the first chapter.

We will start our project by first creating a root directory for it and then cd into the directory so that all our commands are run inside it:

> mkdir blueblog
> cd blueblog

There is no technical reason for this. I just prefer to keep all the files related to a project in one directory as it makes things easier to organize when you have to add further files related to a project, such as designs and other documentation.

Next, we will create a virtual environment to use for the project. Virtual environments are a feature that allow you to create lightweight installations of Python so that each of your projects can have its own installation of all the libraries that it uses. This is useful when you are working on multiple projects at the same time and each project requires a separate version of some library. For example, at work, I once had to work on two projects at the same time. One required Django 1.4; the other required Django 1.9. If I hadn't used virtual environments, it would have been very difficult to keep both versions of Django at the same time.

Virtual environments also allow you to keep your Python environment clean, which is very important when you are finally ready to deploy your application to a production server. When deploying your application to a server, you need to be able to accurately reproduce the same Python environment you had in your development machine. If you don't use a separate virtual environment for each project, you will need to figure out exactly which Python libraries your project uses and then only install those on the production server. With a virtual environment, you no longer need to spend time figuring out which of the installed Python libraries are related to your project. You can just create a list of all the libraries installed in the virtual environment and install them on the production server, confident that you won't miss out on anything or install anything extra that you don't use.

If you want to read more about virtual environments, you can read the official documentation at https://docs.python.org/3/library/venv.html.

To create the virtual environment, we use the pyvenv command:

> pyvenv blueblogEnv 

This creates a new environment inside the blueblogEnv folder. Once we have created the environment, we activate it:

> 
source blueblogEnv/bin/activate

Activating the environment makes sure that any Python commands we run or any libraries we install will use the activated environment. Next, we install Django in our new environment and start our project:

> pip install django
> django-admin.py startproject blueblog src

This creates a directory called src that holds our Django project. You can name the directory anything you want; this is just the convention I prefer.

And that's all there is to the setup of our development environment.