1

I'm a beginner in developing with django, I've been having a problem with a form I've made, I've been searching for similar problems but none of them could solve my problem.

No field of the form render in the HTML but the button renders fine

my form:

from django import forms
from .models import Aluno


class NovoAluno(forms.Form):

    class Meta:
        model = Aluno
        nome = forms.CharField(min_length=15, max_length=100)
        direccion = forms.CharField(min_length=10, max_length=250)
        ciudad = forms.CharField(min_length=3, max_length=50)
        provincia = forms.CharField(min_length=4, max_length=50)
        comunidad = forms.CharField(min_length=4, max_length=50)
        cp = forms.IntegerField()
        faixas = ['Blanco', 'Gris', 'Amarilla', 'Naranja', 'Verde', 'Azul', 'Roxa', 'Marrón', 'Preta']
        graduacion = forms.ChoiceField(choices=faixas)
        inicio = forms.DateInput()
        nacimento = forms.DateInput()
        lic = ['Basica', 'Completa']
        licencia = forms.ChoiceField(choices=lic)
        documento = forms.CharField(min_length=4, max_length=9)
        email = forms.EmailField(min_length=10)
        profesor = forms.CharField(min_length=5, max_length=100)
        centro = forms.CharField(min_length=5, max_length=50)

my views:

from django.shortcuts import render, get_object_or_404, redirect
from .forms import NovoAluno
from .models import Aluno


def home(request):
    return render(request, 'academia/home.html', {})


def novo(request):
    if request.method == "POST":
        form = NovoAluno()
        if form.is_valid():
            form.save()
            return redirect('aluno_detalhes', pk=form.pk)
    else:
        form = NovoAluno()
    return render(request, 'academia/cadastro.html', {'form': form})


def pesquisar(request):
    return render(request, 'academia/pesquisa.html', {})


def aluno_detalhe(request, pk):
    aluno = get_object_or_404(Aluno, pk=pk)
    return render(request, 'academia/aluno.html', {'aluno': aluno})

my model:

from django.db import models


class Aluno(models.Model):
    nome = models.CharField(max_length=100)
    direccion = models.CharField(max_length=250)
    ciudad = models.CharField(max_length=50)
    provincia = models.CharField(max_length=50)
    comunidad = models.CharField(max_length=50)
    cp = models.IntegerField()
    nacimento = models.DateField()
    inicio = models.DateField()
    documento = models.CharField(max_length=9)
    email = models.EmailField()
    profesor = models.CharField(max_length=100)
    centro = models.CharField(max_length=50)
    graduacion = models.CharField(max_length=10)
    licencia = models.CharField(max_length=8)

content block that the form goes

 {% block content %}
    <h1>Novo Aluno:</h1>
    <form method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Cadastrar!</button>
    </form>
 {% endblock %}

Well, thanks in advance, and I'm sorry if it's a dumb mistake, but I can't see where it is...

5
  • Please update whole forms.py code Commented Mar 2, 2018 at 2:02
  • forms.py updated Commented Mar 2, 2018 at 2:25
  • try @brandondavid 's answer,it's right Commented Mar 2, 2018 at 2:32
  • Don't add your solution to your question. Post an answer instead. Commented Mar 2, 2018 at 2:54
  • Oh sry, I'm a total noob Commented Mar 2, 2018 at 3:04

2 Answers 2

1

It looks like you're mixing up forms.Form and forms.ModelForm. Try changing to a model form like this:

from django import forms


class NovoAluno(forms.ModelForm):

    class Meta:
        model = Aluno
        fields = '__all__'

or if you want only specific fields:

class NovoAluno(forms.ModelForm):

        class Meta:
            model = Aluno
            fields = [
            'nome',
            'direcction',
            'ciudad',
]

or using "forms.Form" remove the class Meta and model from what you already have and (fix the indent):

class NovoAluno(forms.Form):
    nome = forms.CharField(min_length=15, max_length=100)
    direccion = forms.CharField(min_length=10, max_length=250)
    ciudad = forms.CharField(min_length=3, max_length=50)
    provincia = forms.CharField(min_length=4, max_length=50)
    comunidad = forms.CharField(min_length=4, max_length=50)
    cp = forms.IntegerField()
    faixas = ['Blanco', 'Gris', 'Amarilla', 'Naranja', 'Verde', 'Azul', 'Roxa',
              'Marrón', 'Preta']
    graduacion = forms.ChoiceField(choices=faixas)
    inicio = forms.DateInput()
    nacimento = forms.DateInput()
    lic = ['Basica', 'Completa']
    licencia = forms.ChoiceField(choices=lic)
    documento = forms.CharField(min_length=4, max_length=9)
    email = forms.EmailField(min_length=10)
    profesor = forms.CharField(min_length=5, max_length=100)
    centro = forms.CharField(min_length=5, max_length=50)

Edit: Everything mentioned above is fine, except when defining choices, they should be a 2-tuple pairs. You can read here. That way you will not get the "ValueError: too many values to unpack (expected 2)"

Class NovoAluno(forms.Form):
    #code suppressed
    faixas = (('1', 'Blanco'), ('2', 'Gris'), ... 
          ..., ('9', 'Marrón'), ('10', 'Preta'))
    graduacion = forms.ChoiceField(choices=faixas)

    ...
    etc
Sign up to request clarification or add additional context in comments.

2 Comments

Now I got something diferent, ValueError at /aluno/new too many values to unpack (expected 2)
nice! For future reference the "too many values error" was caused by the faixas field. Changing it to a tuple of tuples (that act as a key|value pair when the form renders) should solve that problem.
1

Problem solved!!

from django import forms
from .models import Aluno


class NovoAluno(forms.Form):
    model = Aluno
    nome = forms.CharField(min_length=15, max_length=100)
    direccion = forms.CharField(min_length=10, max_length=250)
    ciudad = forms.CharField(min_length=3, max_length=50)
    provincia = forms.CharField(min_length=4, max_length=50)
    comunidad = forms.CharField(min_length=4, max_length=50)
    cp = forms.IntegerField()
    faixas = (
              ('Blanco', 'Blanco'),
              ('Gris', 'Gris'),
              ('Amarilla', 'Amarilla'),
              ('Naranja', 'Naranja'),
              ('Verde', 'Verde'),
              ('Azul', 'Azul'),
              ('Roxa', 'Roxa'),
              ('Marrón', 'Marrón'),
              ('Preta', 'Preta'),
              )
    graduacion = forms.ChoiceField(choices=faixas)
    inicio = forms.DateInput()
    nacimento = forms.DateInput()
    lic = (('Basica', 'Basica'),
           ('Completa', 'Completa'),
           )
    licencia = forms.ChoiceField(choices=lic)
    documento = forms.CharField(min_length=4, max_length=9)
    email = forms.EmailField(min_length=10)
    profesor = forms.CharField(min_length=5, max_length=100)
    centro = forms.CharField(min_length=5, max_length=50)

Erased the class Meta, and then realised that the lists for the forms.ChoiceField weren't in the right format, changed for tuples and everything works fine!

Thx to @brandondavid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.