手动渲染MultiValueField的子字段

时间:2022-04-15 10:18:26

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.

在我的Django 1.7应用程序中,我正在尝试利用MultiValueField类来实现密码/确认密码表单元素,即两个单独的密码字段,要求用户输入然后确认新密码。我已经在我的表单上使用了两个单独的字段和一个clean()方法,但“单个”MultiValueField似乎是维护DRY原则的更好方法,特别是因为我不仅需要在我的用户注册中复制它表单,但也是当用户想要更改其密码时。

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:

但是,我的前端非常具体,而且不像Django的默认表单输出,所以我手动渲染我的表单字段。这很有效 - 直到我进入MultiValueField。对于每个表单字段,我的HTML如下所示:

<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.

我需要为form.password的每个子字段做类似的格式化,但是我尝试过的任何东西都没有给我一个渲染的子字段;我最接近的是{{form.fields.password.fields.0}}在我的模板中,它给出了像 的输出,但是这显然不是渲染的表单字段。 对象在0x7fb619821ef0>

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

是否有一些简单而明显的东西让我失踪,或者我正在尝试做什么在Django中不可能?

1 个解决方案

#1


0  

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

正如farthVader建议的那样,渲染Django中MultiWidget的一部分的代码适合你的问题。

#1


0  

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

正如farthVader建议的那样,渲染Django中MultiWidget的一部分的代码适合你的问题。