django- why after redirecting, the form display “None”

前端 未结 2 443
情话喂你
情话喂你 2021-01-18 18:51

I have a form, after entering the information, based on infomation it filters the database and do some calculation and finally displays the result to a redirected url.

2条回答
  •  孤独总比滥情好
    2021-01-18 19:25

    If you make POST request, and after that you're redirecting user, the next request will have empty POST (since now it it another request). So, it is not a surprising behaviour. If you want to save this data between session, you can save it in the user session, for example.

    You can modify some of you views (which making redirect, I believe) by adding this code:

    Setting a cookie:

    def your_view_which_makes_redirect(request):
      #.. here is your code
      response = HttpResponse( 'blah' )
      request_form_data = request.POST #you need to sanitize/clear this data
      response.set_cookie('form_data', request_form_data)
    

    Getting cookie:

    def your_view_which_renders_page_after_rediret(request):
      if request.COOKIES.has_key('form_data'):
        value = request.COOKIES['form_data'] #this data also should be sanitized
    

    1) Also you can move name of this cookie to the settings because now they are hardcoded and it is now very good practice. Something like settings.SAVED_FORM_NAME_COOIKE_TOKEN 2) You also have to sanitize data from request.POST and request.COOKIES, because user can place there some malicious data (SQL injection and so on).

提交回复
热议问题