user2773013
user2773013

Reputation: 3170

pdb error in django code

I'm new to django environment. I need to debug inherited project. The functionI need to debug is in views.py. Let's call it function x. My environment is redhat, apache, django. How do i debug it using pdb. I tried looking at tutorials, but can't seem to help me.

this is what I run on my redhat command line

python -m pdb /opt/django/projectx/views.py runserver 

when i hit "c" to continue, i'm presented with

ImportError: No module named django.http

Which is the second line of code in views.py. There are 2 functions in view(let's call them x and y), but I need to get to the function x.

Upvotes: 0

Views: 816

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 600059

You don't need to start your server via pdb. You can simply import pdb in your code and set a breakpoint wherever you like:

import pdb; pdb.set_trace()

and you will be dumped into the debugger in the console whenever Django reaches that point.

Upvotes: 1

Nostalg.io
Nostalg.io

Reputation: 3752

Python is installed in almost every environment, but the packages required for each project tend to vary widely. E.g. some projects only work with Django version 1.6, while others are designed for Django version 1.7.

For this reason, some ingenious person created virtual environments. And this is the helpful tutorial done on Red Hat.

Your project is most likely meant to run from within such a virtual environment. The fact that Python can't import django.http suggests that your default environment doesn't even have Django installed.

Developers put the environment folders in different places. Your previous developer may have put it within the project folder, or in another place where other virtual environments are stored.

Look for a folder name with a structure like [name_for_env]/bin/activate.

You would activate this with: source [name_for_env]/bin/activate

Then deactivate with: deactivate

When you are in a virtual environment, you can install missing packages with:

pip install [name_of_package]

OR

You're also using PDB wrong. And that will also give you the import error message. You must run the server with manage.py. This is necessary to setup various Django settings. Run it like so:

python -m pdb manage.py runserver

You can enter in PDB breakpoints in your views.py by following this tutorial.

Upvotes: 2

Related Questions