0

Hola estoy arrancando a programar en Django. Venia bien hasta que me trabe con el tema de los formularios. Lo que quiero hacer es un formulario para insertar request de acceso a la base de datos, para que después sean procesadas. Aca abajo les pego mis lineas de código y mi resultado que no es el deseado ojala me puedan ayudar.

Views.py

from django.shortcuts import render
from django.views.generic import CreateView

from apps.crudRequestAccess.models import RequestUser
from apps.crudRequestAccess.forms import RequestAccessForm
# Create your views here.


class RequestCreate(CreateView):
    model = RequestUser()
    form_class = RequestAccessForm
    template_name = 'crudRequestAccess/request_form.html'

request_form.py

{%  extends "base/base.html" %}

{% block content %}

    <div class="row">
        <div class="col-md-8 col-sm-12">
            {% block main-content %}
                <form method="post">{% csrf_token %}
                    {{ form }}
                    <button class="btn btn-success" type="submit">Save</button>
                    <a class="btn btn-primary" href="#">Cancel</a>
                </form>
            {% endblock %}
        </div>
        <div class="col-md-4 hidden-sm">
            {% block sidebar %}{% endblock %}
        </div>
    </div>

{% endblock %}

Forms.py

from django.forms import ModelForm
from django import forms
from apps.crudRequestAccess.models import RequestUser


class RequestAccessForm(forms.ModelForm):
    class Meta:
        model = RequestUser
        fields = "__all__"

        labels = {
            'First_Name': 'First Name',
            'Last_Name': 'Last Name',
            'Enterprise_Id': 'EnterpriseId',
            'Email': 'Email',
            'Profile': 'Profile',
            'Environment': 'Environment',
            'Approved_By': 'Approved By',
            'Roll_Off': 'Roll Off Date:',
            'Public_Group': 'Public Group',
            'Permission_Set_Licence': 'Permission Set Licence',
            'Permissions_Set': 'Permissions Set',
            'Comments': 'Comments',
            'Status': 'Status',
            'Rejected_By': 'Rejected By',
            'Create_Date': 'Create Date',
            'Create_By': 'Create By',
            'Last_Modify': 'Last Modify',
            'Last_Modify_By': 'Last Modify By'
        }

        widgets = {
            'First_Name': forms.TextInput(attrs={'class': 'form-control'}),
            'Last_Name': forms.TextInput(attrs={'class': 'form-control'}),
            'Enterprise_Id': forms.TextInput(attrs={'class': 'form-control'}),
            'Email': forms.TextInput(attrs={'class': 'form-control'}),
            'Profile': forms.TextInput(attrs={'class': 'form-control'}),
            'Environment': forms.Select(attrs={'class': 'form-control'}),
            'Approved_By': forms.TextInput(attrs={'class': 'form-control'}),
            'Roll_Off': forms.TextInput(attrs={'class': 'form-control'}),
            'Public_Group': forms.TextInput(attrs={'class': 'form-control'}),
            'Permission_Set_Licence': forms.TextInput(attrs={'class': 'form-control'}),
            'Permissions_Set': forms.TextInput(attrs={'class': 'form-control'}),
            'Comments': forms.TextInput(attrs={'class': 'form-control'}),
            'Status': forms.TextInput(attrs={'class': 'form-control'}),
            'Rejected_By': forms.TextInput(attrs={'class': 'form-control'}),
            'Create_Date': forms.TextInput(attrs={'class': 'form-control'}),
            'Create_By': forms.TextInput(attrs={'class': 'form-control'}),
            'Last_Modify': forms.TextInput(attrs={'class': 'form-control'}),
            'Last_Modify_By': forms.TextInput(attrs={'class': 'form-control'})
        }

Models.py

from django.db import models


class RequestUser(models.Model):
    First_Name = models.TextField(max_length=15),
    Last_Name = models.TextField(max_length=15),
    Enterprise_Id = models.CharField(max_length=50),
    Email = models.EmailField(max_length=75),
    Profile = models.TextField(max_length=50),
    Environment = models.CharField(max_length=50),
    Approved_By = models.CharField(max_length=50),
    Roll_Off = models.DateField(),
    Public_Group = models.CharField(max_length=5, default='None'),
    Permission_Set_Licence = models.TextField(max_length=100),
    Permissions_Set = models.TextField(max_length=100),
    Comments = models.CharField(max_length=100),
    Status = models.CharField(max_length=15, default="New", editable=False),
    Rejected_By = models.CharField(max_length=15, editable=False, blank=True),
    Create_Date = models.DateTimeField(auto_now_add=True, blank=False, editable=False),
    Create_By = models.TextField(max_length=50),
    Last_Modify = models.DateTimeField(auto_now=True, blank=True, editable=False),
    Last_Modify_By = models.CharField(max_length=15, editable=False, blank=True)


class PublicGroup(models.Model):
    Id = models.CharField(max_length=50, editable=False),
    GroupName = models.CharField(max_length=50, editable=False),
    GroupsAssociated = models.CharField(max_length=50, editable=False)


class Profile(models.Model):
    Id = models.CharField(max_length=50, editable=False),
    ProfileName = models.CharField(max_length=50, editable=False),
    IdProfile = models.CharField(max_length=50, editable=False)


class PermissionsSet(models.Model):
    Id = models.CharField(max_length=50, editable=False),
    Name = models.CharField(max_length=50, editable=False)


class PermissionSetLicence(models.Model):
    Id = models.CharField(max_length=50, editable=False),
    Name = models.CharField(max_length=50, editable=False)


class PermissionSetLicenceAssignment(models.Model):
    Id = models.CharField(max_length=50, editable=False),
    Id_User = models.CharField(max_length=50, editable=False),
    Name = models.CharField(max_length=50, editable=False)


class PermissionSetAssignment(models.Model):
    Id = models.CharField(max_length=50, editable=False),
    Id_User = models.CharField(max_length=50, editable=False),
    Name = models.CharField(max_length=50, editable=False)


class Approve(models.Model):
    #en lugar de poner uno individual poner el grupo del equipo
    Enterprise_Id = models.CharField(max_length=50),
    Team = models.CharField(max_length=50),


# Data Model Exception List
class ExceptionList(models.Model):
    Enterprise_Id = models.CharField(max_length=50),
    Profile = models.TextField(max_length=50),
    Public_Group = models.CharField(max_length=5, default='None'),
    Comments = models.TextField(max_length=200),
    Approved_By = models.CharField(max_length=50)


# Data Model TestId
class TestId(models.Model):
    First_Name = models.TextField(max_length=15),
    Last_Name = models.TextField(max_length=15),
    Enterprise_Id = models.CharField(max_length=50),
    Email = models.EmailField(max_length=75),
    Profile = models.TextField(max_length=50),
    Public_Group = models.CharField(max_length=5, default='None'),
    Permission_Set_Licence = models.TextField(max_length=100),
    Permissions_Set = models.TextField(max_length=100),


# Users that SpringCM Team request for each integration
class SpringCM(models.Model):
    First_Name = models.TextField(max_length=15),
    Last_Name = models.TextField(max_length=15),
    Enterprise_Id = models.CharField(max_length=50),
    Email = models.EmailField(max_length=75),
    Profile = models.TextField(max_length=50),
    Public_Group = models.CharField(max_length=5, default='None'),
    Permission_Set_Licence = models.TextField(max_length=100),
    Permissions_Set = models.TextField(max_length=100),

me falto mostrar el resultado, es este: introducir la descripción de la imagen aquí

2
  • Bienvenid@ a SOes. Porfavor, has el recorrido de bienvenida. Considera leer el articulo Cómo preguntar y despues borra la respuesta que has publicado y ve a editar tu pregunta para agregar tu imagen. Tambien, parece que el modelo se queda a la mitad, te ha faltado colocar algo? Commented el 17 ene. 2019 a las 21:35
  • Hola, recuerda que puedes realizar ediciones a la pregunta para agregar más información mediante la opción "editar" @user1145543
    – Jorgesys
    Commented el 17 ene. 2019 a las 22:06

1 respuesta 1

0

Te recomiendo que en tu template realizes un for para imprimir cada campo de tu formulario:

{% for row in form %}
    <div class="form-group {% if field.errors %} has-error {% endif %}"> <!-- con la clase has-error podemos hacer visualizar al usuario cuando algo anda mal -->
        <label class="control-label" for="{{ field.html_name }}"> <!-- field.html_name imprime el nombre que django le da al campo -->
            {% if field.field.required %}<i class="fa fa-asterisk text-red"></i> {% endif %}{{ field.label_tag }} <!-- .label_tag es el valor del label que le diste en forms.py -->
        </label>
        {{ field }} <!-- aqui imprimimos el campo -->
        <p class="help-block">{{ field.errors.as_text }} {{ field.field.css_classes }}</p>
    </div>
{% endfor %}

Espero esto te pueda servir. De esta manera es mas facil manipular tu formulario y darle los estilos que mas te convengan/gusten. Puedes comunicarme cual es el resultado para llegar a la solucion optima.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.