CreateView does not save in the database with save ()


Yesterday I asked something about the form where they helped me quickly. Today I come, with the same subject, for that form that what I put in that form is not saved in the database and redirects me to the url but you do not show them to me.

I thought that with save () and within formvalid () it could work but it does not work for me either.

from django.db import models
from django.forms import ModelForm

class Item(models.Model):
    title = models.CharField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

class ItemForm(ModelForm):
    class Meta:
        model = Item
        fields = ['title']

from django.conf.urls import url
from .views import ItemListView, ItemAddView

app_name = 'items'
urlpatterns = [
    url(r'^$', ItemListView.as_view(), name="item"),
    url(r'^agregar/$', ItemAddView.as_view(), name="item_add"),

from django.shortcuts import render

from django.views.generic import ListView, CreateView

from .models import Item, ItemForm

class ItemListView(ListView):
    template_name = 'items/items_list.html'
    model = Item

class ItemAddView(CreateView):
    model = Item
    template_name = 'items/item_form.html'
    form_class = ItemForm
    success_url = 'item'

    def form_valid(self, form):
        return super(ItemAddView, self).form_valid(form)


{% extends "base.html" %}

{% block content %}
    <form action="/" method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Agregar</button>
{% endblock content %


I hope someone can resolve this doubt, greetings!

asked by ikenshu 10.08.2016 в 14:40

1 answer


A form in HTML, is a metaphor of forms or forms on paper: they collect information in fields of different types so that this information can be processed later.

Then, when creating an HTML form you need to indicate which data you want to collect and where the information you collect will be processed.


I insist on the HTML forms, because the problem is related to Django only tangentially, the problem here is that the concepts that have to do with the forms are not clear.

A form, then, collects information in fields of different types, in your particular case:

  • an input field id_title of type text , <input type="text" ...>
  • an input field id_created of type text , <input type='text' ...>

There is also a special field, type submit that sends the form data to place indicated for processing (obviously not a place) indicated in the parameter action of brand <form> .

And here is where your error is.

If we check the line <form> on your form

<form action="/" method="POST">

We see that you are sending the form to the root. Now, in your file we see that the path / is handled by the view ItemListView whose function is not to update the model , but to list the records that model contains.

For your form to work, you must send the data to the view you have created to update the model, that is, to ItemAddView .

In conclusion, your template should look like this:

{% extends "base.html" %}

{% block content %}
  <form action="{% url 'item_add' %}" method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Agregar</button>
{% endblock content %}


  • To learn how HTML forms work: link
  • To know the types of data that a form handles in Django (the widgets ): link
  • To know Django's {% url %} brand, link
answered by 10.08.2016 / 20:58