Search code examples
pythondjangodjango-viewsdjango-urlsurl-mapping

How to get the view that corresponds to a url in Django (using the url)?


How can I get the view that a url maps to? For example:

urlpatterns = patterns('',
                       url(r'^login/$', 'registration.views.Login'),)

How can I use the string 'login/' to return registration.views.Login?

In case I'm not clear, I want to write a function that does the following (I'll call it foo):

>>> foo('login/')
<function Login at 0x010101010>
>>>

I'm writing a script that needs this. Thanks!


Solution

  • Okay, got it. This is how I did it:

    >>> from django.core.urlresolvers import resolve
    >>> foo = resolve('/login/')
    >>> foo.func
    <function Login at 0x010101010>
    >>>
    

    from https://docs.djangoproject.com/en/1.5/ref/urlresolvers/#resolve