万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

DjangoのUpdateViewで成功時の処理

DjangoでUpdateViewを継承したビューでテンプレートを作成した時に、 行き先を設定しないと、

ImproperlyConfigured at /edit/1/
No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model.
Request Method: POST
Request URL:    http://153.120.3.247/demo_dialogue_agent/edit/1/
Django Version: 1.6.4
Exception Type: ImproperlyConfigured
Exception Value:    
No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model.
Exception Location: /usr/local/Python2.7.5/lib/python2.7/site-packages/django/views/generic/edit.py in get_success_url, line 139
Python Executable:  /usr/bin/python
Python Version: 2.7.5
Python Path:    
['/usr/local/Python2.7.5/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/usr/local/Python2.7.5/lib/python2.7/site-packages/pip-6.0.3-py2.7.egg',
 '/usr/local/Python2.7.5/lib/python27.zip',
 '/usr/local/Python2.7.5/lib/python2.7',
 '/usr/local/Python2.7.5/lib/python2.7/plat-linux2',
 '/usr/local/Python2.7.5/lib/python2.7/lib-tk',
 '/usr/local/Python2.7.5/lib/python2.7/lib-old',
 '/usr/local/Python2.7.5/lib/python2.7/lib-dynload',
 '/usr/local/Python2.7.5/lib/python2.7/site-packages',
 '/var/www/django/demo/dialogue_agent/agent']
Server time:    土, 28 2月 2015 10:20:10 +0900

的なエラーになるはず。

https://docs.djangoproject.com/en/1.7/ref/class-based-views/generic-editing/ を見るように、

from django.core.urlresolvers import reverse
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=200)

    def get_absolute_url(self):
        return reverse('author-detail', kwargs={'pk': self.pk})

get_absolute_urlメソッドも実装した方がいい。