How can I change a Django form field value before saving?

前端 未结 5 1017
[愿得一人]
[愿得一人] 2021-01-31 08:29
if request.method == \'POST\':
    userf = UsersModelForm(request.POST)
    username = userf.data[\'username\']
    password = userf.data[\'password\']
    passwordrepea         


        
5条回答
  •  面向向阳花
    2021-01-31 09:25

    You will have problems if you need to fill form from POST, change any form field value and render form again. Here is solution for it:

    class StudentSignUpForm(forms.Form):
      step = forms.IntegerField()
    
      def set_step(self, step):
        data = self.data.copy()
        data['step'] = step
        self.data = data
    

    And then:

    form = StudentSignUpForm(request.POST)
    if form.is_valid() and something():
      form.set_step(2)
      return  render_to_string('form.html', {'form': form})
    

提交回复
热议问题