django forms does not insert data

0

I have a form with a model related to several models, create the form and it is well displayed, but when you submit, nothing happens, that is, the data is not saved in the db, I use python 3.7, django 2.0 and mysql:

Model

class Proyecto(models.Model):
    id_proyecto = models.AutoField(primary_key=True)
    numero_proyecto = models.FloatField(blank=True, null=True)
    descripcion_proyecto = models.CharField(max_length=255, blank=True, null=True)
    os_moebius = models.CharField(max_length=50, blank=True, null=True)
    id_solicitante = models.ForeignKey('Solicitante', models.DO_NOTHING, db_column='id_solicitante', blank=True, null=True)
    id_responsable = models.ForeignKey(Lider, models.DO_NOTHING, db_column='id_responsable', blank=True, null=True)
    id_nodo = models.ForeignKey(Nodo, models.DO_NOTHING, db_column='id_nodo', blank=True, null=True)
    id_ep = models.ForeignKey(EstadoProyecto, models.DO_NOTHING, db_column='id_EP', blank=True, null=True)  # Field name made lowercase.
    id_tp = models.ForeignKey('TipoProyecto', models.DO_NOTHING, db_column='id_TP', blank=True, null=True)  # Field name made lowercase.
    id_sm = models.ForeignKey('ServiceManager', models.DO_NOTHING, db_column='id_SM', blank=True, null=True)  # Field name made lowercase.
    id_herramienta = models.ForeignKey(Herramienta, models.DO_NOTHING, db_column='id_herramienta', blank=True, null=True)
    id_cliente = models.ForeignKey(Cliente, models.DO_NOTHING, db_column='id_cliente', blank=True, null=True)
    observaciones = models.CharField(max_length=255, blank=True, null=True)
    id_agente = models.ForeignKey(Agente, models.DO_NOTHING, db_column='id_agente', blank=True, null=True)
    nombre_tarea = models.CharField(max_length=255, blank=True, null=True)
    periodicidad = models.CharField(max_length=50, blank=True, null=True)
    hora_ejecucion = models.TimeField(blank=True, null=True)
    tiempo_ejecucion_manual = models.IntegerField(blank=True, null=True)
    tiempo_ejecucion_automatico = models.IntegerField(blank=True, null=True)
    
    def __str__(self):
        return self.descripcion_proyecto
    class Meta:
        managed = False
        db_table = 'proyecto'
        ordering = ('-numero_proyecto', )

view:

class ProyectoCreate(CreateView):
    model = Proyecto
    fields = ['numero_proyecto',
            'descripcion_proyecto',
            'os_moebius',
            'id_solicitante',
            'id_responsable',
            'id_nodo',
            'id_ep',
            'id_tp',
            'id_sm',
            'id_herramienta',
            'id_cliente',
            'observaciones',
            'id_agente',
            'nombre_tarea',
            'periodicidad',
            'hora_ejecucion',
            'tiempo_ejecucion_manual',
            'tiempo_ejecucion_automatico']
   

Url:

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

urlpatterns = [
     url(r'^$',views.index , name='index'),
     url(r'^details/(?P<id>\w{0,50})/$', views.details),
     url(r'projects/add/$', views.ProyectoCreate.as_view(), name="project-add")
]

proyecto_form.html

{% extends 'projects/layout.html' %}

{% block content %}

    <div class="container center-align" style="width:35%">
        <div class="row section">
            <div class="col s12 m12 l12">
                <div class="card blue-grey darken-1">
                    <div class="card-content white-text">
                        <span class="card-title">Nuevo Proyecto</span><br><br>
                            <form action="" method="post" enctype="multipart/form-data">
                                {% csrf_token %}
                                {% for fields in form %}
                                        <span class="red-text">{{ fields.error }}</span>
                                        <label>{{ fields.numero_proyecto }}</label>
                                        <div>{{ fields }}</div>
                                {% endfor %}
                                <button type="submit">Enviar</button>
                            </form>
                    </div>                
                </div>
            </div>
        </div>
    </div>
{% endblock %}
    
asked by Jordan Blake Told 31.08.2018 в 18:18
source

1 answer

0

You should encapsulate your "view" in the "forms" folder, which is where the forms are defined, and in "view" or view is where those fields will be collected through the POST method. That's where the save () should be made that will save the data in the database. I do not have much experience in django but I think your fault is that you are not treating the data in the view, but that you have generated the form. Remember that the forms have their specific place in the form folder. I hope this serves as a guide. The view would be something like this:

def post(self, request):
   proyecto_form = ProyectoForm(request.POST)
       if proyecto_form.is_valid():
           proyecto = Proyecto(proyecto_form['numero_proyecto'], 
                            proyecto_form['descripcion_proyecto']....) 
           proyecto.save()
       return render(request, 'aquí el .html hacia donde quieras 
                     redirigir', context={"proyecto": proyecto})
    
answered by 06.09.2018 в 16:40