Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Named URLs and class-based generic views [RSS Feed]

#1 Dec. 3, 2010 19:51:19

Andrew W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Named URLs and class-based generic views


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?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 3, 2010 20:05:35

w.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Named URLs and class-based generic views


On Dec 3, 1:46 pm, Andrew Willey <and...@apt9online.com> wrote:
> 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?

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.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 3, 2010 20:47:39

Andrew W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Named URLs and class-based generic views


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:
> 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?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 3, 2010 21:00:41

w.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Named URLs and class-based generic views


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.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 3, 2010 21:12:09

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Named URLs and class-based generic views


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.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Dec. 3, 2010 21:40:54

Andrew W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Named URLs and class-based generic views


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!

On Dec 3, 3:12 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> 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.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Dec. 3, 2010 21:45:53

w.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Named URLs and class-based generic views


> 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).

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#8 Dec. 3, 2010 22:27:06

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Named URLs and class-based generic views


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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Named URLs and class-based generic views [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support