views:

76

answers:

4

I have an issue where I need to pass in query parameters for a GET request, but Django is not resolving the URL correctly to the view.

My urls.py looks like this:

   from django.conf.urls.defaults import * 
   urlpatterns = patterns('',
       url(r'^confirm_cancel',
          'myapp.views.confirm_cancel_method',
           name='myapp_confirm_cancel'),
       )

When I goto /confirm_cancel?some_id=x I get a 404, telling me "No MyModel matches query." When I set a breakpoint in my view handler, it does not get hit when I goto that url.

However, if I goto /confirm_cancel/x/, my view breakpoint does get hit.

One more thing to note, this worked in Django 1.1, but is now broken since I upgraded to 1.2.

Any thoughts?

Thanks!

A: 

Did you check if this was a case of the trailing slash?

Manoj Govindan
Yep, slash or no slash, it still fails.
LeeMobile
+1  A: 

I don't think the problem is with your url. Are you using a shortcut like get_object_or_4o4 somewhere in your view? For example:

get_object_or_404(MyModel, pk=99)

would result in a "No MyModel matches given query, if there wasn't a record in your table with a primary key of 99.

ars
Yes, I'm using get_object_or_404(), but before that line is hit I put in a debug breakpoint, and that breakpoint is not getting hit at all. So that's not where it's throwing the 404.
LeeMobile
Trying to narrow it down ... The error message you pasted indicates a problem with a *model* not being found, not the URL handler. It might help to paste your view code and the entire stack trace instead.
ars
@ars: +1 good reasoning.
Manoj Govindan
A: 

We need to see what's in the corresponding view function.

Ideally, it should look something like this:

def confirm_cancel_method(request, some_id=None):
    some_id = request.REQUEST.get('some_id', some_id)
    some_record = get_object_or_404(SomeModel, pk=some_id)
    ...

update

Sorry, just saw your note about the breakpoint. One thing I'd recommend is changing the config to this:

from django.conf.urls.defaults import * 
urlpatterns = patterns('',
    url(r'^confirm_cancel/?$',
       'myapp.views.confirm_cancel_method',
        name='myapp_confirm_cancel'),
    )

Adding /?$ at the end means that only /confirm_cancel or /confirm_cancel/ will match the url. Right now because you don't have the ending $ anything starting with confirm_cancel will match. Fixing the pattern will at least resolve this issue.

Jordan Reiter
A: 

I had copied out all the other url patterns in the urls.py in my post.

Turns out that the issue was that I had a r'^(?P\w+)/?$' for one of the urls at the top of the urlpatterns.

Next time I'll learn to paste everything instead of cherry picking what I think are the offending lines of code.

Strange that this did not cause Django 1.1 to break... I guess it was a bug that was fixed in 1.2

LeeMobile
Glad it's fixed. Go ahead and accept your own answer so the question is considered solved (and the solution apparent for other people facing this issue).
ars
I very nearly asked whether there was a matching url pattern earlier in the list.
Jordan Reiter