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.
models.py
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']
urls.py
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"),
]
views.py
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):
form.save()
return super(ItemAddView, self).form_valid(form)
template
{% extends "base.html" %}
{% block content %}
<form action="/" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Agregar</button>
</form>
{% endblock content %
}
I hope someone can resolve this doubt, greetings!