Template does not show me the form made with ModelForm in Django

0

I'm doing a small ToDo app to start practicing some things in Django and I found this little problem that I do not know how to solve until now.

I am using python 3 and django 1.10

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()

    def __str__(self):
        return self.title

class ItemForm(ModelForm):
    class Meta:
        model = Item
        fields = '__all__'

views.py

from django.shortcuts import render

from django.views.generic import ListView, UpdateView

from .models import Item, ItemForm

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

class ItemAdd(UpdateView):
    model = Item
    template_name = 'items/item_form.html'
    form_class = ItemForm
    success_url = '/'

template

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

urls.py

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

urlpatterns = [
    url(r'^$', ItemListView.as_view(), name="item"),
]

All this just shows me the add button that I have placed in the template, but nothing else, maybe I'm doing wrong the form's call in the template, but really no idea.

If someone helps me, I'll appreciate it: P

    
asked by ikenshu 09.08.2016 в 17:19
source

1 answer

0

What happens is that you are calling the template with the view ItemListView but the view containing the form is ItemAdd , so you do not see anything.

Change your urls.py file to the following line:

url(r'^$', ItemAdd.as_view(), name="item"),

That will be enough to draw the form, but there is nowhere to send it. For that you would have to modify it like this:

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

Take note of the path action="/" that receives the data you send.

With this you can update your database, but remember that, although you have a view that shows the content of your database, you do not do anything with it.

    
answered by 09.08.2016 / 21:25
source