8

In my Django 1.7 app, I'm trying to take advantage of the MultiValueField class to implement a password/confirm password form element, i.e. two separate password fields asking the user to enter and then confirm a new password. I already got this working with two separate fields and a clean() method on my form, but the "single" MultiValueField seems a better way of upholding the DRY principle, especially since I'll need to duplicate this not only in my user registration form, but also when users want to change their passwords.

However, my front-end is pretty specific, and not the least bit like Django's default form output, so I'm manually rendering my form fields. This works great -- until I get to the MultiValueField. For each form field, my HTML looks like this:

<div class="row collapse">                                                                                                                                                                                                                                    
    <div class="small-2 columns">                                                                                                                                                                                                                             
        <span class="prefix"><i class="fi-mail"></i></span>                                                                                                                                                                                                   
    </div>                                                                                                                                                                                                                                                    
    <div class="small-10  columns {% if form.email.errors %}error{% endif %}">                                                                                                                                                                                
        {{ form.email }}                                                                                                                                                                                                                                      
        {% if form.email.errors %}<small class="error">{{ form.email.errors }}</small>{% endif %}                                                                                                                                                             
    </div>                                                                                                                                                                                                                                                    
</div>

I need to do similar formatting for each of the subfields of form.password, but nothing I've tried has given me a rendered subfield; the closest I've come is {{ form.fields.password.fields.0 }} in my template, which gives me output like <app.fields.PassField object at 0x7fb619821ef0>, however this obviously isn't a rendered form field.

Is there something simple and obvious that I'm missing, or is what I'm trying to do just not possible in Django?

Kromey
  • 1,202
  • 1
  • 10
  • 13
  • After an hour of clearly segregating validation logic with MultivalueField and MultiWidgets, I got stuck at this place. Were you able to overcome this? I'd appreciate it immensely! :) – farthVader Jun 28 '16 at 20:27
  • 1
    This solution worked for me. http://stackoverflow.com/questions/24866936/render-only-one-part-of-a-multiwidget-in-django – farthVader Jun 28 '16 at 21:19

2 Answers2

0

The code in Render only one part of a MultiWidget in Django is appropriate for your problem, as farthVader suggests.

J_H
  • 17,926
  • 4
  • 24
  • 44
0

I had a similar problem and I ended up solving it this way:

<input type="text" id="{{ form.password.html_name }}_0" name="{{ form.password.html_name }}_0" value="{{ form.password.value.0|default_if_none:'' }}"/>
<input type="text" id="{{ form.password.html_name }}_1" name="{{ form.password.html_name }}_1" value="{{ form.password.value.1|default_if_none:'' }}"/>
moppag
  • 956
  • 9
  • 17