[Answer]-Instance on Django form not working

1👍

Your view is not creating the form for get requests – how are you rendering the initial form?

If the form is not valid, print pessoaForm.errors to see what the problem is. Print request.POST to make sure the expected data is there. It looks like there’s a problem with the sobernome field, so check that especially closely.

0👍

I think the main problem here is the approach you’re using to implememt an “user profile”. See Storing additional information about users. You already are half way.

When you have configured the user profile correctly then you can associate the form to your profile class — Pessoa in this case –.

On the other hand, you are excluding usuario and nome_completo but the last is not a field of Pessoa class.

Also, your Pessoa class has fields which porpuse are the same of some User model fields for instance: primeiro_nome.

Recommendation:

  1. Visite the link I posted.
  2. Review you design, maybe you don’t want to define the field primeiro_nome in the Pessoa class.
  3. Check that the data in request.user.pessoa is what your’re expecting for. Meaning that primeiro_nome and sobrenome fields have some data.

Finally in this answer — at last comment — you can see this OP was struggling with something like your problem.

He said: For the record I also tried user_form = UserForm(request.POST, instance=user, prefix='user'): No error but that always returns an empty form.

Maybe you can try to load your form the way this answer propossed.

Leave a comment