Hi, I have a typical view in which I handle POST and GET requests separately. I’m also using a django form. After taking in the form and validating it, I process the data and display it on the same page again, using render()
, through the same view. However, when I refresh the page, it resubmits the form data. This would be okay if I displayed the same information for the same form data submission, but I query an API which for ease of understanding say returns random numbers (it doesn’t actually do this, I’m just saying that the data returned is different every time, and why I don’t want form resubmission). How can I clear the form data after submission?
The typical pattern for form handling is to do a redirect upon a successful POST.
In this particular case, you can redirect to the current page. That’s going to change your “current request” from the POST used to submit the data to the GET. If you then do a refresh, you’ll be repeating that GET and not the POST.
Is there a way to pass in context variables with a redirect? The POST request is on form submission, the view makes a call to the API and retrieves some information, which then I display on the same template that the form is in. How can I pass in that information to the template with a redirect?
Not as a “context”, no. You would pass them as url parameters - either as components of the URL or as query variables. (The exact answer is going to depend in part on the details of the view.)