0

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Rildoy
  • 11
  • 4
  • Why should it? You've included the template, but you haven't added the form to the context. The form is only sent by the AreaCreate view. – Daniel Roseman Aug 13 '18 at 20:09
  • @Rildoy This is a class-based view and so the form is available in template if form_class is defined. I checked everything with one of my CreateViews and the fields are shown. What about those custom css classes? – Uroš Trstenjak Aug 13 '18 at 20:14
  • Thank you for your comment. In the URL of the template -area_form.html- if the form is shown, the problem is to include it in the other template, as I read that when using createview, it is no longer necessary to use the context or if ?. – Rildoy Aug 13 '18 at 20:16
  • @Rildoy ListView won't show your form unless you add it to the context. See for example something like this https://stackoverflow.com/questions/18664182/is-it-possible-to-have-a-form-in-a-listview-template – Uroš Trstenjak Aug 13 '18 at 20:20
  • Thank you very much @Uroš Trstenjak. Your example was excellent. Thanks too DanielRoseman. – Rildoy Aug 13 '18 at 20:34
  • @UrošTrstenjak . Excuse me can I ask something else or do I have to open a new question? If I wanted to add the edit and delete class to the same view, could it? Since, I have a table where the data appear and I have added buttons to edit and delete as "modal", but I can not find a way to make it work in the same view. – Rildoy Aug 15 '18 at 14:17
  • @Rildoy Why would you like to do it in the same view anyway? Did you mean same template? Your buttons should redirect to approprate urls for UpdateView and DeleteView separated and it will work out of the box. I would agree with this post https://stackoverflow.com/questions/15025300/can-i-make-a-view-using-updateview-and-deleteview-together, which says that update and delete are doable in single view if the view is function based. – Uroš Trstenjak Aug 15 '18 at 15:04

1 Answers1

1

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: ""});
      });
Rildoy
  • 11
  • 4