fusionbox / django-betterforms

Making forms suck less
https://django-betterforms.readthedocs.org/
BSD 2-Clause "Simplified" License
135 stars 54 forks source link

Showing single form fields doesn't work #36

Closed c0d3z3r0 closed 9 years ago

c0d3z3r0 commented 9 years ago

I'm trying to view two ModelForms through MultiModelForm with a FormView. In the template I can't view single fields using e.g. {{ form.username }} while using a loop like below works.

{% for field in form %}
  {{ field }}
{% endfor %}

Edit: {{ form.user.username }} works. Is this intended?

c0d3z3r0 commented 9 years ago

I modified __getitem__ in the MultiForm class but that only works for two forms with unique field names.

def __getitem__(self, key):
        try:
            return self.forms[key]
        except KeyError:
            return {x.name: x for x in 
                    chain.from_iterable(self.forms.values())}[key]
c0d3z3r0 commented 9 years ago

My solution raises more problems than it solves... maybe that was a bad idea ;)

julianandrews commented 9 years ago

{{ form.user.username }} works. Is this intended?

Yup, it sounds as if you're worked this all out. For fine grained access to fields the {{ form.user.username }} approach is the the intended approach. Direct access to fields causes problems if forms share field names.

I'm going to go ahead and close the bug!

rockymeza commented 9 years ago

Perhaps some documentation might help here?

-rocky 2015年10月12日 上午3:39,"Julian Andrews" notifications@github.com写道:

{{ form.user.username }} works. Is this intended?

Yup, it sounds as if you're worked this all out. For fine grained access to fields the {{ form.user.username }} approach is the the intended approach. Direct access to fields causes problems if forms share field names.

I'm going to go ahead and close the bug!

— Reply to this email directly or view it on GitHub https://github.com/fusionbox/django-betterforms/issues/36#issuecomment-147238631 .

julianandrews commented 9 years ago

I was thinking about that. The docs do have a snippet that describes this in a view context, which sort of implies the use in templates"

form = UserProfileMultiForm()
# get the Field object
form['user'].fields['name']
# get the BoundField object
form['user']['name']

But possibly that section could be fleshed out to include a short template example to make it really explicit.