Select widget in Django does not appear in the form

Asked

Viewed 717 times

0

my signup page is not showing the selection fields, my code is as follows:

Forms.py

class InscricaoForm(forms.Form):

    VINCULO = [
        (u'b', u'Bolsista'),
        (u'e', u'Estagiário'),
        (u't', u'Terceiro'),
        (u's', u'Servidor'),
    ]

    SALAS = [
        (u'1', u'SysAdmin'),
        (u'2', u'Help Desk'),
    ]
    vinculo = forms.ChoiceField(choices=(VINCULO), label='Vinculo')
    registro = forms.IntegerField(label=_('Registro'))
    nome = forms.CharField(label=_('Nome'))
    email_inpe = forms.EmailField(label=_('Email @ Inpe'))
    email = forms.EmailField(label=_('Email'))
    data_admissao = forms.DateField(label=_('Data de Admissao'))

    sala = forms.CharField(widget=forms.Select(choices=SALAS),label=_('Salas'))

    ramal = forms.CharField(label=_('Ramal'))


class Bolsista_EstagiarioForm(InscricaoForm):
    data_fim_contrato = forms.DateField(label=_('Data de Fim de Contrato'))

class TerceiroForm(InscricaoForm):
    empresa = forms.CharField(label=_('Empresa'))

class ServidorForm(InscricaoForm):
    tipo = forms.CharField(label=_('Tipo de Servidor'))


class InscricaoForm(forms.ModelForm):
        class Meta:
            model = Inscricao
            fields = '__all__'

py.models

VINCULO = (
    (u'b', u'Bolsista'),
    (u'e', u'Estagiário'),
    (u't', u'Terceiro'),
    (u's', u'Servidor'),
    )

SALAS = (
    (u'1', u'SysAdmin'),
    (u'2', u'Help Desk'),
)

EMPRESAS = (
    (u'1', u'Cray'),
    (u'2', u'Outros'),
)

SERVIDOR = (
    (u'1', u'Técnico'),
    (u'2', u'Tecnologista'),
)


class Inscricao(models.Model):

    # criando relacionamento 1:1 com o user do django
        usuario = models.OneToOneField(User)
        vinculo = models.CharField(max_length=100, choices=VINCULO)
        registro = models.IntegerField()
        email_inpe = models.EmailField(blank=True)
        data_admissao = models.DateField()
        sala = models.CharField(max_length=100, choices=SALAS)
        ramal = models.CharField(max_length=30)
        criado_em = models.DateTimeField('criado em', auto_now_add=True)

        class Meta:
                abstract = True

class Bolsista_Estagiario(Inscricao):
    data_fim_contrato = models.DateField()

    class Meta:
        verbose_name_plural = "Bolsistas e Estagiários"


class Terceiro(Inscricao):
    empresa = models.CharField(max_length=30, choices=EMPRESAS)

    class Meta:
        verbose_name_plural = "Terceiros"

class Servidor(Inscricao):
    tipo = models.CharField(max_length=30, choices=SERVIDOR)

    class Meta:
        verbose_name_plural = "Servidores"

py views.

def inscricao(request):
    form_class = InscricaoForm

    # new logic!
    if request.method == 'POST':
        form = form_class(data=request.POST)

        if form.is_valid():
            vinculo= request.POST.get(
                'vinculo'
            , '')
            registro= request.POST.get(
                'registro'
            , '')
            nome = request.POST.get(
                'nome'
            , '')
            email_inpe= request.POST.get(
                'email_inpe'
            , '')
            email= request.POST.get(
                'email'
            , '')
            data_admissao= request.POST.get(
                'data_admissao'
            , '')


            form_content = request.POST.get('content', '')

            # Email the profile with the
            # contact information
            template = get_template('contact_template.txt')


            content = template.render({'vinculo': vinculo,'registro': registro,
                                              'nome': nome,'email_inpe': email_inpe,
                                              'email': email,'data_admissao': data_admissao,'form_content': form_content})

            email = EmailMessage(
                "Novo usuário cadastrado",
                content,
                "Portal Cptec" +'',
                ['[email protected]'],
                headers = {'Reply-To': '[email protected]' }
            )
            email.send()

            messages.success(request, 'Profile details updated.')

admin py.

class InscricaoAdmin(UserAdmin):

    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'email', 'first_name', 'last_name', 'password1', 'password2'),
        }),
    )


admin.site.register(Bolsista_Estagiario)
admin.site.register(Servidor)
admin.site.register(Terceiro)
#admin.site.register(Inscricao)
admin.site.unregister(User)

admin.site.register(User, InscricaoAdmin)

inserir a descrição da imagem aqui

  • Already tried to use modelForm, it is the most indicated in your case, because it adapts automatically compared to your model.

1 answer

0


I don’t know if I understand the doubt correctly, but try to adapt your forms.py according to the fragment below:

from .models import Inscricao
# Outros imports

class InscricaoForm(forms.Form):

    # Choices
    VINCULO = [
        (u'b', u'Bolsista'),
        (u'e', u'Estagiário'),
        (u't', u'Terceiro'),
        (u's', u'Servidor'),
    ]

    SALAS = [
        (u'1', u'SysAdmin'),
        (u'2', u'Help Desk'),
    ]

    # Fields
    # Outros fields ...     
    vinculo = forms.ChoiceField(choices=(VINCULO), label='Vinculo')    
    # Outros fields ...          
    sala = forms.ChoiceField(choices=(SALAS), label='Sala')

See what cities in .models is a tuple of tuples, but for ChoiceField needs to be a list of tuples.

  • I understood and tested here but not yet showed the field for selection, look at you see in the image that the fields that have Choices does not appear anything

  • Edit the question and put the adapted code. Actually, in the image are not showing fields ChoiceFields, how are you rendering the form? put, too, the code where you do it.

  • This is exactly what I want to solve, in the form should appear the Choicefields field with the options and nothing is appearing

  • OK, I repeat. Show the code with the changes you made, in the image only fields appear CharField (these fields do not present any options) which is in the code you display, you need to change the fields CharField for ChoiceField as I put it in the answer, show the code where you do it. Also, it would be necessary to know how you are rendering the form, show the code where you render it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.