Django url debugger - django

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.

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

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.

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.

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.

Related

Catch url without parameters

I'm having issues catching url while excluding parameters (what's after ? sign)
So for the following links
/alfa/wp-includes/js/jquery/jquery.js
/beta/wp-includes/js/jquery/jquery.js?parameter=value
I need the following
alfa /wp-includes/js/jquery/jquery.js
beta /wp-includes/js/jquery/jquery.js
So far I came up with the following but it always catch full link including parameters, which I don't need
^(/alfa|beta)?(/wp-(content|admin|includes).*)
Try this (assuming your inputs always ended in .js)
(/(alfa|beta)(/wp-(content|admin|includes).*))(\.js|\?)
Or improving upon ctwheels' comment:
^/(alfa|beta)([^?\s]*)(\.js|\?)
I'm not sure if it's 100% what you need but I ran that query in regexpal.com and it works as you described.
Try this :
(\/(alfa|beta)[^\?]+)
https://regex101.com/r/AmgHTS/8

Is there an easy way to tell which URLs are unused?

I have a Django project which is entering its fourth year of development. Over those four years, a number of URLs (and therefore view functions) have become obsolete. Instead of deleting them as I go, I've left them in a "just in case" attitude. Now I'm looking to clean out the unused URLs and unused view functions.
Is there any easy way to do this, or is it just a matter of grunting through all the code to figure it out? I'm nervous about deleting something and not realizing it's important until a few weeks/months later.
The idea is to iterate over urlpatterns and check that the status_code is 200:
class BasicTestCase(TestCase):
def test_url_availability(self):
for url in urls.urlpatterns:
self.assertEqual(self.client.get(reverse('%s' % url.name)).status_code,
200)
I understand that it might not work in all cases, since there could be urls with "dynamic" nature with dynamic url parts, but, it should give you the basic idea. Note that reverse() also accepts arguments in case you need to pass arguments to the underlying view.
Also see: Finding unused Django code to remove.
Hope that helps.
You are looking for coverage, which is basically "how much of the code written is actually being used".
Coverage analysis is usually done with tests - to find how what percentage of your code is covered by tests - but there is nothing stopping you to do the same to find out what views are orphaned.
Rather than repeat the process, #sk1p already answered it at How to get coverage data from a django app when running in gunicorn.

Hash anchor tag causing errors in URL

On very rate occasions, my error log is showing the following error:
"You specified a Fuseaction of registrationaction#close which is not defined in Circuit public."
The full link is:"http://myUrl/index.cfm?do=public.registrationAction#close"
As you can see, the has merely points to an anchor (close) on the page.
This code is working 99% of the time, but on the odd occasion, Coldfusion / Fusebox throws this error out.
Why is this happening?
Could it be related to the device accessing my page somehow? Like a cell phone or Apple product that for some reason does handle hashes the way I am expecting it to?
Could it be javascript / JQuery being disabled?
Any guidance would be appreciated
Thanks
I used to see stuff like that. Older versions of Internet Explorer were not handling the hashtag properly when there were URL parameters. The best solution I could come up with was kludgey at best, but basically it forced the anchor tag to separate from the URL parameter.
http://myUrl/index.cfm?do=public.registrationAction&#close
I'm not sure there is a simple answer to this. We get odd exceptions all the time on our site for all sorts of reasons. Sometimes it's people not using the site the way you expect and sometimes it stuff like you mention such as user-agent edge cases etc.
Basically you need to start to gather evidence and see what comes up that's unusual with these requests.
So to start: do you catch exceptions in you application? If so dumping all scopes (CGI/CLIENT/FORM/URL/SESSION) in an email along with the full exception and emailing them to a custom emails address (such as errors#yourdomain.com) will give you a reference you can square up to your error times and this might give you a hint as to the real issue.
Hope that helps some!

Regex to change method call parameter

Regexs make me cry, so, I came here for help.
I'm looking for some tips on Find & Replace in Panic's Coda. I know the F&R
is pretty advance but I'm just looking for the best way to do this.
I'm trying to rewrite a 'template engine' (very basic) I have going on with a
webapp I'm coding in PHP (CodeIgniter).
Currently I'm calling my template like so:
$this->load->view('subviews/template/headerview');
$this->load->view('subviews/template/menuview');
$this->load->view('subviews/template/sidebar');
$this->load->view('The-View-I-Want-To-Load'); // This is the important line
$this->load->view('subviews/template/footerview');
However it's inefficient using five lines of code every time I want to
load up a different page, in every different controller. So I rewrote it like this:
$data['view'] = 'The-View-I-Want-To-Load';
$this->load->view('template',$data);
That way if I need to make any major changes to the design it can
easily be done from the template.php view file (which contains the header, menu, sidebar views etc. etc.).
However I use the previous 5-lines all over the place in many
different controllers and functions. So, my question is --- How can I
find and replace the old template engine (5 lines of code) for the new
one - substituting in the name of the view in the important, unique
line for the one in $data['view]?
Does that make any sense?! If not I'll try and rephrase! I mean, is there a way of doing this via a Regex or something? Or am I on completely the wrong lines here?
your regex will look something like this :
\$this->load->view\('subviews/template/headerview'\);\n\$this->load->view\('subviews/template/menuview'\);\n\$this->load->view\('subviews/template/sidebar'\);\n\$this->load->view\('([^']*)'\);\n\$this->load->view\('subviews/template/footerview'\);
and replace with
\$data['view'] = '$1';\n\$this->load->view('template',\$data);

Django's Satchmo and flatpages issue

I'm having a problem with configuring Flatpages in Satchmo. I've used them before, in a pure django app, but now it just doesn't work, returning 301 http error when I'm trying to enter a flatpage-configured site.
What I've done to configure it:
added the middleware "django.contrib.flatpages.middleware.FlatpageFallbackMiddleware" to MIDDLEWARE_CLASSES as last in the list,
configured example pages in admin module.
Simply what the docs say about flatpages config.
I'm feeling helpless. Don't know how could I debug this issue. Any thoughts on that?
And of course help appreciated.
Thanks to suggestion by Peter I've managed to narrow the problem to my urls.py file, for the satchmo shop.
The urlpatterns has only one entry:
(r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}),
This version does not work and moreover interfere with flatpages. But disabling flatpages from MIDDLEWARE_CLASSES and adding it to urls.py like the snippet below works:
(r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage'),
(r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}),
However next problem is with the redirection from / to /shop/. With the above config it results in infinite loop.
Perhaps you know the reason for that behavior (redirect overriding flatpage) and maybe You could suggest some working solution to this problem or what should be done with requests to /.
It returns 301? That's Page Moved Permanently (HttpResponsePermanentRedirect) and there are no references to that in the flatpages directory so I don't think it's coming from there. In fact there are only about 5 references to HttpResponsePermanentRedirect in all of the standard 1.1.1 release.
Possible approaches:
Comment out the FlatPages middleware and see if the error changes (I'm betting it won't).
Try changing the order of your MIDDLEWARE classes and see if things change.
When presenting a problem like this it's better to get very specific by showing the exact code from applicable portions of settings.py (or whatever) and by giving other things like precise URLs and the urls.py patterns you are trying to match.
Update:
OK, some random thoughts:
The pattern (r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage'), will match anything. Any patterns after it will never be seen.
flatpages doesn't work by being called directly, it does its magic in middleware. It looks for 404 responses (Page Not Found) and then looks to see if that path exists in its table. If so, it calls a view that renders the page, etc. etc. If it doesn't find a match it let's the 404 continue on through middleware processing.
The pattern (r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}), will match anything (I just tested it). If you want to match an empty path, use r('^$', etc.). This is the source of your infinite loop.
If you are new to regular expressions the Django urls.py file can seem like F*cking Magic. I recommend starting very simply and add one rule at a time. Do some quick tests to ensure that the new rule a) matches stuff you want it to match, and b) doesn't match stuff it shouldn't. In particular, make sure that some of the rules that occur later in the file are still reachable. In this case they wouldn't have been which should have raised a red flag.