reverse in urlpatterns

84 views
Skip to first unread message

Baurzhan Ismagulov

unread,
Dec 10, 2009, 7:10:35 AM12/10/09
to django...@googlegroups.com
Hello,

Can I use reverse() in urlpatterns, similar to the following?

urlpatterns = patterns('',
...
(r'^apps/$', list_detail.object_list,
{'queryset': App.objects.all()},
'app-list'),
(r'^app/new/$': create_update.create_object,
{'model': App, 'post_save_redirect': reverse('app-list')},
'app-new'),
...
)

When I try this, I get NoReverseMatch at /apps/: Reverse for 'app-list'
with arguments '()' and keyword arguments '{}' not found.

I'd like to avoid hard-coding URLs. Is there a way to do that?

I'm aware that I could probably abuse get_absolute_url for that.
However, I'll need to implement more complex workflows where the same
URL is going to be "called" from different pages on the site and will
have to return back to the "calling" page (not unlike admin's popup
windows, albeit without popups). What is the best way to do that?

I'm using Django 1.0.

Thanks in advance,
--
Baurzhan Ismagulov
http://www.kz-easy.com/

Javier Guerra

unread,
Dec 10, 2009, 9:04:10 AM12/10/09
to django...@googlegroups.com
On Thu, Dec 10, 2009 at 7:10 AM, Baurzhan Ismagulov <i...@radix50.net> wrote:
> When I try this, I get NoReverseMatch at /apps/: Reverse for 'app-list'
> with arguments '()' and keyword arguments '{}' not found.
>
> I'd like to avoid hard-coding URLs. Is there a way to do that?


the problem is that the call to reverse('app-list') is happening
_before_ the construction of the URL list.

i don't know much about Django internals, but i'd at least try
separating 'app-new' in a different call to patterns() than
'app-list'.

something like:

urlpatterns = patterns('',
...
(r'^apps/$', list_detail.object_list,
{'queryset': App.objects.all()},
'app-list')),
)

urlpatterns += patterns('',
(r'^app/new/$': create_update.create_object,
{'model': App, 'post_save_redirect': reverse('app-list')},
'app-new'),
...
)


not sure if would work, thought. it's quite possible that Django
still has work to do after the whole urls.py execution to make
reverse() work.


--
Javier

Baurzhan Ismagulov

unread,
Dec 12, 2009, 9:44:37 AM12/12/09
to django...@googlegroups.com
Hello Javier,

On Thu, Dec 10, 2009 at 09:04:10AM -0500, Javier Guerra wrote:
> urlpatterns = patterns('',
> ...
> (r'^apps/$', list_detail.object_list,
> {'queryset': App.objects.all()},
> 'app-list')),
> )
>
> urlpatterns += patterns('',
> (r'^app/new/$': create_update.create_object,
> {'model': App, 'post_save_redirect': reverse('app-list')},
> 'app-new'),
> ...
> )

Thanks for the idea. I've tried it and got the following error:

File "/var/lib/python-support/python2.5/django/core/handlers/base.py" in get_response
77. request.path_info)
File "/var/lib/python-support/python2.5/django/core/urlresolvers.py" in resolve
179. for pattern in self.urlconf_module.urlpatterns:
File "/var/lib/python-support/python2.5/django/core/urlresolvers.py" in _get_urlconf_module
198. self._urlconf_module = __import__(self.urlconf_name, {}, {}, [''])
File "/home/ibr/w/work/mcs/j/rc/src/rc/urls.py" in <module>
23. 'post_save_redirect': reverse('app-list')

Exception Type: TypeError at /admin/his/person/add/

With kind regards,
Reply all
Reply to author
Forward
0 new messages