There are a lot of questions on SO asking nearly this very same question, but it seems every one has a different issue at the heart of it. With that in mind, I have combed through the questions from the past several years without luck finding a solution to my own unique issue. I think I just need a pair of eyes more experienced than mine.
I'm trying to render a form on a profile information edit page. The form is supposed to draw from two models, the main auth.User and a profile model connected through OneToOne. The issue is that the form not only doesn't render in HTML, but also shows no sign of life in the manage.py shell prompt. First, I'll show you the form itself, then I'll show the output of the prompt. I'm not going to show my views unless asked for the sake of simplicity and because I'm ruling them out for the moment.
forms.py:
from django import forms
from django.contrib.auth.models import User
from .models import People, Pick # this is actually for a later forms, which don't work either
class ProfileForm(forms.Form):
class Meta:
model = User
fields = ['people__usr_img', 'email', 'password', 'people__middle_name', 'people__gender', 'people__preference',]
And my shell output:
$ > python manage.py shell
...Python meta stuff...
>>> from apps.catch.forms import ProfileForm
>>> ProfileForm()
<ProfileForm bound=False, valid=Unknown, fields=()>
>>> form = ProfileForm()
>>> form.as_p()
''
>>> for field in form:
... print("Field")
...
>>>
It seems to me that what's at the heart of this issue is the fields=() declaration. I have the fields defined explicitly in Meta, so why am I getting nothing here? It should be mentioned that I am having the same issue with my other forms as well.
{{ form.as_p }}, but in interactive shellform.as_p()forms.ModelForminsteadforms.Form. The ModelForm class allows you to use a metaclass to override the target model (i.e. User). Also it forces you to specify a tuple with the desired fields. Please note that that fields must match the User fields.