Rildoy
Rildoy

Reputation: 11

Why doesn't include show the fields of a form when reusing a template?

Good morning, I'm new to Django, I'm trying to include a template form, but it does not show the fields, just the save button. Use Django 2.1. I summarize the code, so please can you help me. since you need to reuse the form in other templates.

models.py

from django.db import models
class Area(models.Model):
    nombre = models.CharField(max_length=45)
    descripcion = models.CharField(max_length=100, null=True, blank=True)

    def __str__(self):
        return self.nombre

views.py

class AreaCreate(CreateView):
    model = Area
    form_class = AreaForm
    template_name = 'area/area_form.html'

class AreaList(ListView):
    model = Area
    template_name = 'area/area_list.html'

forms.py

class AreaForm(forms.ModelForm):
    class Meta:
        model = Area

        fields = (
                'nombre',
                'descripcion',
        )
        widgets = {
                'nombre': forms.TextInput(attrs={'class': 'form-control', 'placeholder':'Area'}),
                'descripcion': forms.TextInput(attrs={'class': 'form-control', 'placeholder':'Descripción'}),
        }

area_form.html

<form action="" method="POST"> {% csrf_token %}
   <div class="form-row">
     <div class="col-sm-4 campoform">
     {{ form.nombre }}
     </div>
     <div class="col-sm-6 campoform">
     {{ form.descripcion }}
     </div>
     <div class="col-sm-2 campoform">
     <button class="btn btn-primary" type="submit" style="width:100%">Guardar</button>
     </div>
  </div>
</form>

area_list.html --- I am not adding the complete list.html code for a better understanding.

<div>{% include "area/area_form.html" %}</div>

The result in area_list.html is that it shows only the button.

Upvotes: 0

Views: 94

Answers (1)

Rildoy
Rildoy

Reputation: 11

Sorry for the delay in responding. I solved it by calling the form with a modal. Using the library "bootstrap_modal_forms".

 #View
class GestionarAreas(generic.ListView):
        model = Area
        context_object_name = 'areas'
        template_name = 'areas/gestionar_areas.html'
        paginate_by = 10  

class CrearArea(generic.CreateView):
        template_name = 'areas/crear_area.html'
        form_class = AreaForm
        success_url = reverse_lazy('gestionar_areas')

#urls
path('', login_required(views.GestionarAreas.as_view()),name='gestionar_areas'),
path('crear/', login_required(views.CrearArea.as_view()), name='crear_area'),

#HTML
<div class="col-sm-auto">
    <button class="crear-area btn btn-primary" type="button"name="button">
    <span class="fa fa-plus mr-2"></span>Crear Area</button>
</div>

#Script
// Boton Crear Area
 $(".crear-area").modalForm({formURL: "{% url 'crear_area' %}", successURL: ""});

// Boton Actualizar Area
 $(".editar-area").each(function () {
        $(this).modalForm({formURL: $(this).data('id'), successURL: ""});
      });

Upvotes: 1

Related Questions