Wednesday, August 29, 2012

Passing default arguments to class based views

Im trying to come up with a basic CRUD, nested resource implementation.

I cant figure out how to pass a default argument (the content type id, in my case) to the view class.

Below is what Ive used. Any pointers to accomplish this, or if this is total crap, any better way to 

accomplish this?

    urlpatterns = patterns('',
   url
(
       regex
= '^/?$',
       view
=  ParentResourceListView.as_view(),
       name
= 'parent_resource_list'
   
),

   url
(
       regex
= r'^(?P<pk>\d+)/$',
       view
=  ParentResourceDetailView.as_view(),
       name
= 'parent_resource_detail'
   
),
   url
(
       regex
= r'^new/$',
       view
=  ParentResourceCreateView.as_view(),
       name
= 'parent_resource_create'
   
),

   url
(
       regex
= '^(?P<pk>\d+)/delete/$',
       view
=  ParentResourceDeleteView.as_view(),
       name
= 'parent_resource_delete'
   
),
   url
(
       regex
= '^(?P<pk>\d+)/edit/$',
       view
=  ParentResourceUpdateView.as_view(),
       name
= 'parent_resource_update'
   
),
)


urlpatterns
+= patterns('',
                   url
(
    regex
= r'^(?P<object_id>\d+)/child_resources$',
    view
=  ChildResourceListView.as_view(),
    name
= 'parent_resource_child_resource_list',
    kwargs
= {
       
"content_type",
       
ContentType.objects.get_for_model(ParentResource).id
       
}
   
),
                   url
(
    regex
= r'^(?P<object_id>\d+)/child_resources/(?P<pk>\d+)/$',
    view
=  ChildResourceDetailView.as_view(),
    name
= 'parent_resource_child_resource_detail',
    kwargs
= {
       
"content_type",
       
ContentType.objects.get_for_model(ParentResource).id
       
}
   
),
                   url
(
    regex
= r'^(?P<object_id>\d+)/child_resources/new/$',
    view
=  ChildResourceCreateView.as_view(),
    name
= 'parent_resource_child_resource_create',
    kwargs
={
       
"content_type",
       
ContentType.objects.get_for_model(ParentResource).id
       
}
   
),

                   url
(
    regex
= '^(?P<object_id>\d+)/child_resource/(?P<pk>\d+)/delete/$',
    view
=  ChildResourceDeleteView.as_view(),
    name
= 'parent_resource_child_resource_delete',
    kwargs
={
       
"content_type",
       
ContentType.objects.get_for_model(ParentResource).id
       
}
   
),
                   url
(
    regex
= '^(?P<object_id>\d+)/child_resource/(?P<pk>\d+)/edit/$',
    view
=  ChildResourceUpdateView.as_view(),
    name
= 'parent_resource_child_resource_update',
    kwargs
={
       
"content_type",
       
ContentType.objects.get_for_model(ParentResource).id
       
}
   
),
                   
)

Ive also asked this on SO:

http://stackoverflow.com/questions/12172120/passing-default-arguments-to-class-based-views

--
- Aziz M. Bookwala

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

No comments:

Post a Comment