开发者

Django Url Not Resolving with Query Parameters

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!


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.


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.


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<my_id>\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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜