42

I was wondering what is the 'best' way of passing data between views. Is it better to create invisible fields and pass it using POST or should I encode it in my URLS? Or is there a better/easier way of doing this? Sorry if this question is stupid, I'm pretty new to web programming :)

Thanks

2
  • I dont know if best can be determined from this broad of a question. Could you give examples of two views? You can do anything based on the type of data. Session keys, hidden form fields, query strings. Session keys could work. Commented Oct 14, 2011 at 4:47
  • 3
    Sessions is the answer! There is no simpler and better way. Session data can be saved in database, files or in cache. Commented Oct 14, 2011 at 6:47

1 Answer 1

58

There are different ways to pass data between views. Actually this is not much different that the problem of passing data between 2 different scripts & of course some concepts of inter-process communication come in as well. Some things that come to mind are -

  1. GET request - First request hits view1->send data to browser -> browser redirects to view2
  2. POST request - (as you suggested) Same flow as above but is suitable when more data is involved
  3. Django session variables - This is the simplest to implement
  4. Client-side cookies - Can be used but there is limitations of how much data can be stored.
  5. Shared memory at web server level- Tricky but can be done.
  6. REST API's - If you can have a stand-alone server, then that server can REST API's to invoke views.
  7. Message queues - Again if a stand-alone server is possible maybe even message queues would work. i.e. first view (API) takes requests and pushes it to a queue and some other process can pop messages off and hit your second view (another API). This would decouple first and second view API's and possibly manage load better.
  8. Cache - Maybe a cache like memcached can act as mediator. But then if one is going this route, its better to use Django sessions as it hides a whole lot of implementation details but if scale is a concern, memcached or redis are good options.
  9. Persistent storage - store data in some persistent storage mechanism like mysql. This decouples your request taking part (probably a client facing API) from processing part by having a DB in the middle.
  10. NoSql storages - if speed of writes are in other order of hundreds of thousands per sec, then MySql performance would become bottleneck (there are ways to get around by tweaking mysql config but its not easy). Then considering NoSql DB's could be an alternative. e.g: dynamoDB, Redis, HBase etc.
  11. Stream Processing - like Storm or AWS Kinesis could be an option if your use-case is real-time computation. In fact you could use AWS Lambda in the middle as a server-less compute module which would read off and call your second view API.
  12. Write data into a file - then the next view can read from that file (real ugly). This probably should never ever be done but putting this point here as something that should not be done.

Cant think of any more. Will update if i get any. Hope this helps in someway.

Sign up to request clarification or add additional context in comments.

5 Comments

nice list. Sessions is a good place to start. Chainging variables from view to view using GETS and POSTS is messy
@pastylegs agreed, GETS & POSTS can get messy fast. Sessions are built for this kind of operations in mind. My post reflects that opinion. Please add anything I have probably missed...
That file one (6) is as dirty as it sounds. I recommend removal of that from the awesome list.
@MohitC yup its real ugly. kept it for answer completeness. anyway I hear you, moved point 6 to last and put a disclaimer for that.
What happens when the data is not JSON serializable? In this case, you can not store de data in the session. Sometimes, it is useful to define your variables globally, for example, for Machine Learning models.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.