0

I have been trying to use "UpdateView" in django generic views. What I am trying to achieve is to return to the same "UpdateView" page. My codes are as follows:
My urls

#urls.py    
urlpatterns = patterns(
    '',
    url(r'^code/(?P<pk>\d+)/update/', CodeUpdate.as_view(model=Code, template_name='code_update.html'),name='update',),
    )

my views

#views.py
class CodeUpdate(UpdateView):
    def get_success_url(self):
        pass

After clicking the update button the I expected the destination to be

/code/18/update/

however it turns out to be

/code/18/update/None

How do I remove "None" at the end? Thanks.

Now if I do this:

#views.py
class CodeUpdate(UpdateView):
    def get_success_url(self):
        return reverse('update')

I will get this error.

Reverse for 'update' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'/code/(?P\d+)/update/']

How can I pass the 'pk' value as the argument in the reverse function.

Thanks.

elvinado
  • 3
  • 3

1 Answers1

1

Once update is successful, it will redirect to url returned by get_success_url(). As you are not returning anything in that function, you are redirected to .../update/None.

Either return appropriate url from that method or remove that method for default behavior.

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • You're right on the cause. The default behavior is to redirect to the result of the `get_absolute_url` method of the updated object, which probably isn't what the OP is looking for. To redirect to the update view of the object again, give the URL pattern a name. Then use `reverse` to look up the URL for it and return it from `get_success_url`. – Peter DeGlopper Dec 19 '13 at 04:14
  • If `success_url` is `None` then `get_absolute_url` will be called. – iMom0 Dec 19 '13 at 04:25
  • Hi @PeterDeGlopper . I was trying to get object.id as the argument for the reverse function. It works if I hard code it into the `reverse('update',args=(11,))`, how can I do it dynamically. Thanks. – elvinado Dec 19 '13 at 05:53
  • `UpdateView` saves the object you're updating as `self.object`, so `reverse('update', args=(self.object.id))` should do it. – Peter DeGlopper Dec 19 '13 at 06:03