Hi folks,

Something I find myself writing for each new Django project is a small
wrapper for the django.core.urlresolvers.reverse function.  It usually
looks something like this:

    from django.core.urlresolvers import reverse

    def absolute_url(viewname, *args, **kwargs):
        return reverse(viewname, args=args or [], kwargs=kwargs or {})

It means I can have that warm, fuzzy feeling when I see my views don't
have any hard-coded URLs.  For example:

    def some_view(request, id):
        some_object = get_object_or_404(SomeModel, id=id)
        if request.method == 'POST':
            # Some code that changes the database here.
            return HttpResponseRedirect(absolute_url(some_other_view))
        else:
            # Render the page here.

Assuming the URL pattern for the view above is '/event/(?P<id>\d+)/',
a couple of other examples  are:

    >>> some_id = 1
    >>> absolute_url(some_view, id=some_id)
    '/event/1/'
    >>> absolute_url(some_view, some_id)
    '/event/1/'

Is there any chance this could be included in Django?  It might sit
nicely in somewhere like django.shortcuts.

A nice side-effect would be that it could be used in place of the few
hard-coded URLs that are still hanging around in Django — in
django.contrib.auth for instance:

    LOGIN_URL = absolute_url('django.contrib.auth.views.login')

Is it worth me submitting a patch?

M.

-- 
Matt Riggott (mailto:[EMAIL PROTECTED]).
Dictated but not read.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to