Django: Save and restore a form's state using a cookie

前端 未结 2 511
忘了有多久
忘了有多久 2021-02-05 15:58

I want to make a form that once submitted, on each next rendering of the form for the same user, the form will be already filled out with the data from the last time the user su

2条回答
  •  無奈伤痛
    2021-02-05 16:28

    This already sounds like a good plan. But if there is no pressure on using cookies, I would resort to using the session instead. That way you won't hit a size limit on cookies, and transmitting cookies with higher size can slow down your response time.

    form = MyForm(initial=request.session.get('form_data'))
    ...
    if form.is_valid():
        request.session['form_data'] = form.cleaned_data
    

提交回复
热议问题