[Answer]-Django CreateView does not save object

1👍

According to the error, you have the following URL pattern defined:

metas/(?P\d+)/control$

…which should be metas/(?P<pk>\d+)/control$

Note that this is different from the given patterns above:

urlpatterns = patterns(
    'metas.views',
    url(r'^$', 'home', name='metas_index'),

    url(r'^(?P<pk>\d+)/control$', MetaDetalle.as_view(), name='metas_detalle'),
    url(r'^add-meta$', 'agregar_meta', name='metas_add'),
)

If I had to guess, you are doing something like the below in the root urls.py:

urlpatterns = patterns(
    '',
    url(r'^metas/', include('metas.urls')),
    # Bad line with bad regex below!
    url(r'metas/(?P\d+)/control$', MetaDetalle.as_view(), name='metas_detalle'),
)

Actually, I found the problem:

https://github.com/SGC-Tlaxcala/sgc-metas/blob/e5a6c8e7a54f833795c46d5ece438b219460bf47/src/metas/models.py#L167-L177

class MetasSPE(models.Model):
    ...
    def save(self, **kwargs):
        """
        Se sobre-escribe el método `save()` para guardar la descripción con html.
        :param kwargs: Parámetros en clave
        :return: nada
        """
        from markdown import markdown
        self.descripcion_html = markdown(
            self.descripcion, outpu_format='html5', lazy_ol=True
        )

    def __str__(self):
        ...

You forgot to call super() on your overidden save() method, which means your model will never save:

class MetasSPE(models.Model):
    ...
    def save(self, **kwargs):
        """
        Se sobre-escribe el método `save()` para guardar la descripción con html.
        :param kwargs: Parámetros en clave
        :return: nada
        """
        from markdown import markdown
        self.descripcion_html = markdown(
            self.descripcion, outpu_format='html5', lazy_ol=True
        )
        super(MetasSPE, self).save(**kwargs)

Leave a comment