How do I access data from local storage in Django views?

Viewed 11037

How do I access the value of key "store1" inside local storage in my Django views.py??

HTML, in Django templates --> index.html

-User will key in input and button will activate local storage function

<input type="text" id="firstID">
<button onclick="myFunction()">LocalStorage</button>

Javascript

-User input is saved in variable which is used to for local storage

var siteName = document.getElementById('firstID');
function myFunction() {
  localStorage.setItem('store1', siteName.value);
2 Answers

LocalStorage is client storage in your browser. Your *.py files will be executed in server. So you can not access them directly. You can save them as cookie, or put them to server via ajax request.

As Bendy Zhang has said you might like to use cookies.

In Javascript set your cookie

var value = "some_value";
document.cookie="key="+value;

Then in Django Views use it

key = request.COOKIES.get('key')

Now you have your key value in the varible you can send it as a header (in case of auth token) or another value which you might want to use.

See here for inspiration: https://stackoverflow.com/a/5113697/3904109

Related