0

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.

user2773013
  • 3,102
  • 8
  • 38
  • 58

2 Answers2

2

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.

Nostalg.io
  • 3,662
  • 1
  • 28
  • 31
  • that seems to work. now when i press "c" it tells me the program finished and will be restarted. how do i invoke a view.py in from certain directory, which i think looks like a package – user2773013 Jan 21 '15 at 22:27
  • `views.py` files are part of the MVC layout in Django. Some people refer to it as MVT (Model-View-Template) in Django. The model, which describes the data and generates the database table, is in `models.py`. The views are in `views.py`. And the templates are usually `.html` files in a templates folder. The magic that takes an http request and coordinates everything is usually found in `urls.py`. Look for a `urls.py` config file. It should have a URL regex that points to your view. Then load that URL with the server running. E.g. `localhost:8000/app_name/index/` – Nostalg.io Jan 22 '15 at 02:54
1

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895