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.

Using pdb to debug Django views

You will often come across problems in your Django applications that are not immediately clear. When I get stuck with a tough bug, especially when it's inside a Django view, I use the Python debugger, which is built into Python, to step through my view code and debug the problem. To do so, you'll need to put this line of code into your view right before the point where you think the problem exists:

import pdb; pdb.set_trace()

Then, the next time you load the page associated with that view, you'll see that your browser appears to not load anything. This is because your Django application is now paused. If you look in the console where you ran the runserver command, you should see a prompt for pdb. In the prompt, you can type the name of any variable available in the current Python scope (usually the scope of the view that you are debugging) and it will print the current value of that variable. You can also run a bunch of other debugging commands. For a complete list of available features, take a look at the documentation for the Python debugger at https://docs.python.org/3/library/pdb.html.

A good Stack Overflow question with useful answers that lists some other debugging techniques is http://stackoverflow.com/questions/1118183/how-to-debug-in-django-the-good-way.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.212