Noufal Ibrahim
Noufal Ibrahim

Reputation: 72755

Django url debugger

I'm developing a django application and over time, the URLs have grown. I have a lot of them with me now and due to some change I made, one view started to malfunction. When I try to GET http://example.com/foo/edit_profile, it's supposed to execute a view certain view function X but it's executing Y instead. Somewhere the url routing is messing up and I can't figure it out. I used the django.core.urlresolvers.resolve method to try it from the shell and I can confirm that the URL is getting wrongly resolved. However, I don't know how to debug this and pinpoint the problem.

Ideally, I'd like to see something like "tested this pattern", "tested this pattern" etc. till it finally finds the correct one and I can then look around where it resolved. I can't find anything like this.

Isn't this a common problem for larger projects? What do people do?

Update

I know how the system works and how to look through the URLs one by one. That's what I'm trying to do. This question is basically asking for a shortcut.

Upvotes: 29

Views: 12399

Answers (4)

Sergio Morstabilini
Sergio Morstabilini

Reputation: 2055

have you already tried to run

manage.py show_urls

after installing django_extensions?

http://vimeo.com/1720508 - watch from 06:58.

This should give you in what order the url resolution is attempted.

Hope this helps

Upvotes: 42

hughdbrown
hughdbrown

Reputation: 49013

I would comment out the patterns in your url.py until you get a 404 error when you try to navigate to foo. If that pattern is an include, I would recurse down that and comment out lines in that url.py. Eventually you will know exactly which pattern is matching.

Then I would take the view function that it is calling and hard code that. If it is using a generic view or something subtle, I'd make it as obvious and direct as possible. At that point, you should know which rule is matching and why and what code it is executing in the view.

Upvotes: 4

Andre Bossard
Andre Bossard

Reputation: 6281

You can assume, that it goes through the urlpatterns from top to bottom and the first one that matches will be executed.

As you know which view is executed (Y) think about that:

  • if Y is before X: the patterns of Y matches the url (but shouldn't)
  • if X is before Y: the patterns of X doesn't match the url (but should)

Can you provide some more explicit examples of your URLConf? Than I can give you a more explicit answer.

Upvotes: 1

Rajeev J Sebastian
Rajeev J Sebastian

Reputation: 1

Look at your urlconfs, find which urlpattern invokes your view Y and see if the regexp is more general than it ought to be. Try to comment out the urlpattern which causes the false match and see if it correctly matches X.

Typically, this is not a problem for me, but it does occur. Always keep more specific patterns before general ones. Use static prefixes to divide your url namespace, to prevent false matches.

Upvotes: 0

Related Questions