Pass dictionary object to view from template

It is very common to pass dictionary objects to templates. But how to pass the same dictionary data to some view using get or post request? For my usecase I am passing a dict object to template and using it to fill table data but depending on the need I want to pass that data to another view either using get request or post request. I have tried various things but nothing works so far. Is there any suggestions on how this can be done.

Also, I am new to this forum so let me know if this is not the right place to ask these types of question. My search to find the solution has lead me here. Thanks in advance for help.

First, this is the right place.

However, to answer this, it might be helpful to get into the specifics of what you’re really trying to accomplish here.

The most direct answer to your question is that you can submit data through a post request as a JSON object.

But, the root question is where is this request coming from? Are you looking to have one view call another view? Or is this data being submitted from the browser? Or from something else? What specifically have you tried?

Ken

Here is my exact usecase. I am trying to create a patient Report page. Where a patient uploads his/her medical report, that report is then passed to a view which handles the report and creates a dictionary that contains all of its TestNames, values and units. This data is not saved to server. That dictionary is then passed to the template to show results to patient. If everything is fine with the results then I want to pass that data (dictionary) to another view which can save it to server.

I hope that my explanation is clear.

Ok, so you are going to need to save that data somewhere.

A view receives a request and returns a response. Once that view returns its response, there is no place to keep that data other than -

  • The database
  • The user’s session
  • The system cache facility

So once you’ve sent the response to the patient, you don’t have that data any more to send to another view.
I’m assuming that you’re showing the results of the data to the patient. They’re then (perhaps) clicking “Ok” or “I Agree” or something like that. It’s that event that is causing this second view to be invoked.
That second view needs to get the data from somewhere - either submitted by the browser or retrieved from one of the three storage locations identified above.

Hi, @KenWhitesell thanks for your suggestions. I followed your suggestion of user’s session and after working on it I got a working solution to the problem. I am documenting the steps I took for future reference.

To understand or learn about Django session I first referred Django documentation and then followed this article.

After understanding a new concept I redefined the problem I was facing to say

  • I need to send dictionary object to another view

The article (linked above) should provide all the implementation-related details. But basically it is I am saving dictionary object to request.session['data'] and then simply accessed in another view.

1 Like