Named URLs and class-based generic views

70 views
Skip to first unread message

Andrew Willey

unread,
Dec 3, 2010, 2:46:29 PM12/3/10
to Django users
Can someone point me in the right direction here?

How does one use class-based generic views and named URLs?

Example (doesn't work):

> urls.py
> urlpatterns += patterns('myapp.views',
> (r'^location/$', MyGenericView.as_view()),
> )

> views.py
> class MyGenericView(TemplateView):
> template_name = 'example.html'

Now, I can make that work if I did this instead in
> urls.py
> from myapp.views import *
> urlpatterns += patterns('',
> (r'^location/$', MyGenericView.as_view()),
> )

That approach just feels weird, am I doing it wrong or is that the new
normal?

wayne

unread,
Dec 3, 2010, 3:05:29 PM12/3/10
to Django users
No, that looks right to me. You import the view in the second case,
which means you can pass it like an object, as you do.

Andrew Willey

unread,
Dec 3, 2010, 3:47:32 PM12/3/10
to Django users
Forgot to in mention the actual question from the subject.

Is there a "blessed" way to use class-based views with named urls in
the
> url(r'^location/$', view='WouldLoveToHaveClassBasedView.as_view', name='name_for_convenience')

On Dec 3, 1:46 pm, Andrew Willey <and...@apt9online.com> wrote:

wayne

unread,
Dec 3, 2010, 4:00:34 PM12/3/10
to Django users
On Dec 3, 2:47 pm, Andrew Willey <and...@apt9online.com> wrote:
> Forgot to in mention the actual question from the subject.
>
> Is there a "blessed" way to use class-based views with named urls in
> the
>
> > url(r'^location/$', view='WouldLoveToHaveClassBasedView.as_view', name='name_for_convenience')
>

Oh, I see ( I think ;) ). I don't know, but I am guessing no.
However, you could decorate the view and use the decorator as some
sort of dispatcher, although I am not sure that would really give you
what you want.

Daniel Roseman

unread,
Dec 3, 2010, 4:12:03 PM12/3/10
to Django users
On Dec 3, 8:47 pm, Andrew Willey <and...@apt9online.com> wrote:
> Forgot to in mention the actual question from the subject.
>
> Is there a "blessed" way to use class-based views with named urls in
> the
>
> > url(r'^location/$', view='WouldLoveToHaveClassBasedView.as_view', name='name_for_convenience')
>

I don't understand what one thing has got to do with the other. You
can still use a named URL even if you use the object rather than a
string to refer to it:

url(r'^location/$', view=WouldLoveToHaveClassBasedView.as_view(),
name='name_for_convenience')

It's the `url` function that allows you to use the name kwarg, not
anything to do with the view function that's being called.
--
DR.

Andrew Willey

unread,
Dec 3, 2010, 4:40:47 PM12/3/10
to Django users
So it works as

> url(r'^location/$', WouldLoveToHaveClassBasedView.as_view(), name='name_for_convenience')

Didn't realize I could just pass the class object. Makes sense.
Guess I'm getting old and change fearing.

I think the only thing that's bugging me is that you have to include
the whole views.py in your url conf. Just feels less graceful than
having the dispatcher nab what it needs on demand. I'll get over it.

Unless someone has a better idea!

wayne

unread,
Dec 3, 2010, 4:45:47 PM12/3/10
to Django users

> I think the only thing that's bugging me is that you have to include
> the whole views.py in your url conf.  Just feels less graceful than
> having the dispatcher nab what it needs on demand.  I'll get over it.
>

Yeah, it does feel less graceful, doesn't it? That said, there might
be a way, I just don't know it.

I wouldn't see why you couldn't do it, just like before (specify the
string name)--I just didn't recall seeing that example in the docs for
class based views. Then again, I'm just trying to get my boss to move
us over to 1.2, so I haven't really looked at trunk or projected 1.3
stuff yet (like the new views).

Łukasz Rekucki

unread,
Dec 3, 2010, 5:23:50 PM12/3/10
to django...@googlegroups.com
On 3 December 2010 22:40, Andrew Willey <and...@apt9online.com> wrote:
> So it works as
>
>> url(r'^location/$', WouldLoveToHaveClassBasedView.as_view(), name='name_for_convenience')
>
> Didn't realize I could just pass the class object.  Makes sense.
> Guess I'm getting old and change fearing.

To be clear here - you're not passing the class object here -
as_view() returns a normal function that takes care of everything:
creating a View instance and passing request and URL params to it's
dispatch method. Class-based views get no special handling in Django,
so you could just copy the 'django.views.generic' package to your
Django<1.3 project and use them (I did :).

>
> I think the only thing that's bugging me is that you have to include
> the whole views.py in your url conf.  Just feels less graceful than
> having the dispatcher nab what it needs on demand.  I'll get over it.

You should avoid doing "from ... import *" and just explicitly name
the views you need. It's easier to see which views are actually used
this way. If you really want to use the string syntax:

# views.py

my_view = MyView.as_view()

# urls.py

urlpatterns('myapp.views',
url(r'some_pattern/', 'my_view', name="myapp-my_view")
)

There is not way to use a string like 'myapp.views.MyView' - you need
the alias. Also, you most likely want to name all your URL patterns
that use class-based views or you won't have a way to use reverse()
with them.

--
Łukasz Rekucki

Reply all
Reply to author
Forward
0 new messages