1

I have been using function based views for quite some time but never used class based views until now. My current project requires a lot of common code across multiple views and hence I am exploring class based views so that I can use inheritance.

My requirement is to direct a url to a particular function in the class. Something similar to what can be done on django admin. In Django admin, I have used get_urls method in a class inherited from ModelAdmin to point a url to a class method.

def get_urls(self):
    try:
        from django.conf.urls.defaults import patterns, url

        urls = super(MyModelAdmin, self).get_urls() 

        my_urls = patterns('',
                       url(r'^myurl/(?P<obj_id>\d+)/$',
                           self.admin_site.admin_view(self.myFunction),
                           name="my_function"),
                         )

        return my_urls + urls
    except Exception, e:
        logger.error(e)

The method myFunction simply calls a method in views.py and passes the request object and the object id. I documentation is here

Is it possible to do a similar thing with class based views? From my search so far I couldn't find any documentation which shows the possibility of passing a function pointer to the as_view method, the way we can do the admin_view method. Please guide me or point me at the right documentation.

3
  • I'm not sure if I understood what you are trying to achieve. Are you trying to use class-based views with Django admin app? or with your own apps? If the latter is the case, then calling as_view method should do the work. Something like url(r'^myurl/(?P<obj_id>\d+)/$', MyClassBasedView.as_view(), name="my_function"),) Commented Jun 18, 2015 at 14:25
  • @kaveh: Thanks for the prompt reply. I am trying to use class based views with my own app. I was just comparing it with admin since I felt admin is quite similar to class based views. Regarding your suggestion, isn't that the way for TemplateView? I intend to map urls to my custom class methods. Commented Jun 18, 2015 at 14:35
  • calling as_view works with all CBVs; they all inherit from django.views.generic.View class; you can use it with your own custom classes inheriting from either the base or other provided classes. Commented Jun 18, 2015 at 14:46

1 Answer 1

1

The admin views are very different to the rest of Django's class based views for historical reasons, so moving an implementation from one to the other can be tricky.

Regular CBV are meant to be hooked up to a particular url pattern, they aren't designed to dispatch different urls to different views, so implementing a get_urls method doesn't really make sense.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.