Create/update/delete generic views

119 views
Skip to first unread message

David

unread,
Sep 13, 2011, 7:40:59 AM9/13/11
to Django users
Hello

This is my code so far from urls.py

from django.views.generic import ListView, create_update

url(r'^reportwriting/types/edit/(?P<object_id>\d+)/?$',
create_update.update_object(
model=Type
)),

I am trying to use the new CRUD generic views but am repeatedly
getting the following error:

TypeError at /reportwriting/types/edit/1/
update_object() takes at least 1 argument (1 given)

I think syntactically I must be screwing up, but am not having much
progress using the Django docs.

Thanks for any assistance.

Daniel Roseman

unread,
Sep 13, 2011, 8:15:42 AM9/13/11
to django...@googlegroups.com
create_update.update_object is one of the *old* function views. They work the same as any other functional view: you don't *call* them in urls.py, you reference them and pass extra context arguments as necessary.

The new class-based CRUD views are documented here:
You probably want UpdateView, which like all class-based view needs to be referenced in urls.py as UpdateView.as_view(model=Foo)
--
DR.

David

unread,
Sep 13, 2011, 8:53:49 AM9/13/11
to Django users
Hi Daniel

That is very helpful and I have it working now. Is there a way to have
my success_url go to my ListView class-based view as it isn't named?

url(r'^reportwriting/types/$', ListView.as_view(
model=Type, paginate_by=2
)),
url(r'^reportwriting/types/edit/(?P<pk>\d+)/?$',
UpdateView.as_view(
model=Type,
success_url='/success/'
)),

Thanks again.

David

unread,
Sep 13, 2011, 8:58:29 AM9/13/11
to Django users
Or alternatively to redirect to the update form that has just been
submitted.

David

unread,
Sep 13, 2011, 9:07:46 AM9/13/11
to Django users
Got it

url(r'^reportwriting/types/$', ListView.as_view(
model=Type, paginate_by=2
)),
url(r'^reportwriting/types/edit/(?P<pk>\d+)/?$',
UpdateView.as_view(
model=Type,
success_url='/reportwriting/types/'
)),

or

url(r'^reportwriting/types/$', ListView.as_view(
model=Type, paginate_by=2
)),
url(r'^reportwriting/types/edit/(?P<pk>\d+)/?$',
UpdateView.as_view(
model=Type,
success_url='/reportwriting/types/edit/%(id)i/'
)),

Now just to work out how to redirect to the same page that was
paginated. ie/ page=2.
Reply all
Reply to author
Forward
0 new messages