Ok, so I've created a fairly simple form:

class OrganizationItemForm(AuditModelForm):
    selected      = forms.BooleanField()  
    extraRequired = forms.BooleanField(widget=forms.HiddenInput)
    multiLineInfo = forms.BooleanField(widget=forms.HiddenInput)
    
    def __init__(self, *args, **kwargs):
        super(AuditModelForm, self).__init__(*args, **kwargs)
        if self["multiLineInfo"].value():
            self.fields["descr"].widget = 
forms.Textarea(attrs={"class":"descriptionEdit"})
        else:
            self.fields["descr"].widget = 
forms.TextInput(attrs={"class":"itemDescrEdit"})     
        self.fields["descr"].required = self["extraRequired"].value()
        
    class Meta:
        model = OrganizationItem

I use this as a single checkbox entry that could optionally have either a 
single line edit, multi-line edit, or no text box at all

The applicable section of the template looks like this:


        <ul>
        {% for item in cat.items %}            
            <li><label>{{ item.form.selected }} {{ item.label }}</label>
            {% if item.form.extraRequired.value or item.descrLabel %}  
                {% if item.form.multiLineInfo.value %}
                    <br>
                {% else %}
                    &ndash;
                {% endif %}
                <span class="listItemLabel{% if item.form.extraRequired.data 
%} required{% endif %}">{{ item.descrLabel }}</span>
                {% if item.form.multiLineInfo.value %}
                    <br>
                {% else %}
                    &nbsp;
                {% endif %}
                {{ item.form.descr }}
            {% else %}
                {{ item.form.descr.as_hidden }}
            {% endif %}
            {{ item.form.extraRequired }}
            {{ item.form.multiLineInfo }}
            </li>
        {% endfor %}
        </ul> 

Keep in mind, this is a work in progress. I've not integrated the error 
output yet. Right now my problem is that if I simply click submit to test 
all of the error conditions, the value of the hidden fields is a string. 
This seems to be because to_python is only called for the cleaned value. The 
cleaned data is only available after the form passes validation.

What is the best practice for carrying data like this in a form and using it 
in the templates to govern conditionals?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/nvSquVbDnmMJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to