Saturday, May 31, 2014

UpdateView, ModelForm - form not redirecting problem

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALUDr%2B2vb7kbFGJdnGtP_h%3De50ONvn%2B2x1TjDr95%3D2mV6gFruw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment