[Django]-Django: How to pre-populate FormView with dynamic (non-model) data?

97👍

You can override the FormView class’s ‘get_initial’ method. See here for more info,

e.g.

def get_initial(self):
    """
    Returns the initial data to use for forms on this view.
    """
    initial = super().get_initial()

    initial['my_form_field1'] = self.request.something

    return initial

‘get_initial’ should return a dictionary where the keys are the names of the fields on the form and the values are the initial values to use when showing the form to the user.

Leave a comment