1

I´m working with the Form Wizard which is alright but right now its not really pretty.

My Views.py is based on the docs:

class ContactWizard(SessionWizardView):
form_list = [DurationForm, ContactForm2]
def done(self, form_list, **kwargs):
    return render(self.request, 'done.html', {
        'form_data': [form.cleaned_data for form in form_list],
    })

My done.html contains:

{% extends "base.html" %}
{% block content %}
<h1> Done! </h1>
{% endblock %}

My forms are defined in forms.py:

from django import forms

class DurationForm(forms.Form):
     Duration_in_secounds = forms.IntegerField()

class FrequencyForm(forms.Form):
     Frequency_in_time_interval = forms.IntegerField()

Now i wonder how the forms even render because i never load them the way i used (for example with

{{form.as_p}}

Because whatever i change in my done.html doesnt affect the forms created with the wizard i have no idea how to add css to them.

Could any of you help me get whats going on here? (sorry if the question is stupid/ already asked - its quite late here and i couldn´t find anything for the last two hours)

2 Answers 2

3

I think it doesn't matter it's a Form Wizard. You can just create a base class that you have DurationForm and ContactForm2 extend from. For example:

def __init__(self, *args, **kwargs):
    super(BaseFormClass, self).__init__(*args, **kwargs)

    # specific field
    self.fields['name'].widget.attrs['class'] = 'my_class'

    # all fields on form
    for field in self.fields:
         self.fields[field].widget.attrs['class'] = 'my_class'

Not tested.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! Even though i abandoned the wizard i am using something similar to your solution for my form . Theres only one thing still missing: Adding the class in attrs worked for every form except the checkbox form. This might be because i need to specifiy a type in the html but adding this to the attrs didnt work out for me :/ forms.MultipleChoiceField(widget = forms.CheckboxSelectMultiple(attrs={'class':'form-group', 'type':'checkbox',}), choices = CHOICES_AGE) Any idea what i´m missing? If not i will greate another question for this (googling didnt help until now :b)
Maybe try adding in the attr in the MultipleChoiceField instead
then i get an unexpected argument error - i´ll comment the soution here too once i found it =)
How are you calling it?
the form itself? with {{ form.as_p}} The error occurs when i try ` Target_Age = forms.MultipleChoiceField(widget = forms.CheckboxSelectMultiple(), choices = CHOICES_AGE, attrs={'class':'form-check', 'input_type':'checkbox',} ) `
|
0

You should add load static to your template (html):

{% load staticfiles %}
<link href="{% static 'myapp/css/my.css' %}" rel="stylesheet">

This is where the css, scripts, etc. are loaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.