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

后端 未结 1 661
难免孤独
难免孤独 2020-12-08 14:02

I have a FormView view, with some additional GET context supplied using get_context_data():

class SignUpView(FormView):
    template_name = \'pages_fixed/acc         


        
相关标签:
1条回答
  • 2020-12-08 14:40

    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.

    0 讨论(0)
提交回复
热议问题