Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Extra context in formsets - additional examples / explanation for rendering field values as text in formsets? #1297

Open
rzrwolf opened this issue Aug 2, 2022 · 0 comments

Comments

@rzrwolf
Copy link

rzrwolf commented Aug 2, 2022

Hello!

I am following the readme about Formsets with CrispyForms and have one question in relation to example:

class ExampleFormSetHelper(FormHelper):
    def __init__(self, *args, **kwargs):
        super(FormHelper, self).__init__(*args, **kwargs)
        self.form_method = 'post'
        self.layout = Layout(
            HTML('{% if forloop.first %} Only display text on the first iteration... {% endif %}'),
            Fieldset('Item: {{forloop.counter}}', 'field'),
            'favorite_color',
            'favorite_food',
        )
        self.add_input(Submit('submit', 'Save'))

Particularly - HTML('{% if forloop.first %} Only display text on the first iteration... {% endif %}'),

Is there any way to use this forloop for rendering just values of QuerySet which is sent to form as context, something like:

HTML('{% MyQuerySet.forloop.counter %}

I just need to add textual captions of the QuerySet to the form.. See the screenshot example.
Capture

The code i use for helper layout

class ProbesFormHelper(FormHelper):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.label_class = "d-none"
        self.form_method = 'POST'

        self.layout = Layout(
            Row(
                Field('probe', css_class='form-group col-md-12 mb-0 d-none'),
                Column('value', css_class='form-group col-md-1 mb-0'),
                Column('inUse', css_class='form-group col-md-2 mb-0'),
                HTML('{{ probes }}'), #HERE i try to render out context 'probes' - need the textual value

                css_class="form-row align-items-center"

            ),
        )

        self.form_tag = False
        self.render_required_fields = True

Or maybe there is a way to render individual form items from formset in template somehow, so i can build layout here and use one 'SUBMIT' for all of them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant