UpdateView, ModelForm - form not redirecting problem

28 views
Skip to first unread message

Erick Brockes

unread,
May 31, 2014, 12:23:49 PM5/31/14
to django...@googlegroups.com
Hello,

I have the following situation.

If I don't refernce a ModelForm in my UpdateView class, the update process works correctly. When I reference the ModelForm in order to customize the fields, the form do not works correctly (do not redirect to the address specified).

forms.py

class PessoaForm(forms.ModelForm):
    dia_nascimento = forms.ChoiceField(choices=[(x, x) for x in range(1, 32)], required=False)
    mes_nascimento = forms.ChoiceField(choices=[(x, x) for x in range(1, 13)], required=False)
    ano_nascimento = forms.CharField(max_length=4, widget=forms.TextInput(attrs={'size':'3'}), required=False)
    dia_falecimento = forms.ChoiceField(choices=[(x, x) for x in range(1, 32)], required=False)
    mes_falecimento = forms.ChoiceField(choices=[(x, x) for x in range(1, 13)], required=False)
    ano_falecimento = forms.CharField(max_length=4, widget=forms.TextInput(attrs={'size':'3'}), required=False)

    class Meta:
        model = Pessoa

--------
models.py

class Pessoa(models.Model):

    SEXO = (
                ('M', _('Masculino')),
                ('F', _('Feminino')),
        )

    nome = models.CharField(_('nome'), max_length=100, null=False, blank=False)
    sobrenome = models.CharField(_('sobrenome'), max_length=300, null=False, blank=False)
    sobrenome_casada = models.CharField(_('sobrenome casada'), max_length=300, null=True, blank=True)
    falecido = models.BooleanField(default=False)
    dia_nascimento = models.PositiveSmallIntegerField(_('dia de nascimento'), null=True, blank=True)
    mes_nascimento = models.PositiveSmallIntegerField(_('mês de nascimento'), null=True, blank=True)
    ano_nascimento = models.CharField(max_length=4, null=True, blank=True, validators=[
            RegexValidator(
                r'^[0-9]*$',
                _('Somente números são permitidos.'),
                'Invalid Number'
            ),
        ],
    )
    ...
    dia_falecimento = models.PositiveSmallIntegerField(_('dia de falecimento'), null=True, blank=True)
    mes_falecimento = models.PositiveSmallIntegerField(_('mês de nascimento'), null=True, blank=True)
    ano_falecimento = models.CharField(max_length=4, null=True, blank=True, validators=[
            RegexValidator(
                r'^[0-9]*$',
                _('Somente números são permitidos.'),
                'Invalid Number'
            ),
        ],
    )
    ...   
    class Meta:
        ordering = ['sobrenome', 'nome']
        verbose_name = _(u'pessoa')
        verbose_name_plural = _(u'pessoas')
   
    def __unicode__(self):
        if(self == None):
            return ""
        else:
            return self.nome + " " + self.sobrenome


-----
views.py

class PessoaUpdate(UpdateView):
    model = Pessoa
    fields = ['nome','sobrenome','sobrenome_casada','sexo','dia_nascimento','mes_nascimento','ano_nascimento','falecido', 'dia_falecimento','mes_falecimento','ano_falecimento']
    form_class = PessoaForm

    def get_success_url(self):
        return "/pessoa/nascimento/"+str(self.kwargs['pk'])+"/"


----------

Does anyone have a clue?

Thanks in advance.

Erick

Dan Gentry

unread,
Jun 1, 2014, 8:41:26 PM6/1/14
to django...@googlegroups.com
What does it do instead?


Reply all
Reply to author
Forward
0 new messages