简体   繁体   English

如何使用 javascript 而不是 Django 会话将表单数据(Django 表单)存储到浏览器的本地存储?

[英]How to store Form Data (a Django form) to browser's local storage, using javascript and not Django sessions?

I want to take the data from the user through a form (a Django form) and store it in the browser's local storage, using javascript and without using Django sessions.我想通过表单(Django 表单)从用户那里获取数据并将其存储在浏览器的本地存储中,使用 javascript 而不使用 Django 会话。

My code looks like:我的代码看起来像:

./forms.py : ./forms.py :

class digitize(forms.Form):

    medicinename = forms.CharField(label='Medicinename', max_length=100)
    quantity = forms.CharField(label='Quantity', max_length=100)

./e_commerce.html : ./e_commerce.html :

<form id="digitizer_form" role="form" action="/MediciansDigitizer/Presc/" method="post" enctype="multipart/form-data">

   {% csrf_token %}

   <h3 class="prod_title">Add Detail</h3>

   {{iform.as_table}}{{ dform.as_p }}

   <div class="">
   <button type="Submit" class="btn btn-default btn-lg">Digitize</button>
   </div>

</form> 

<script>

        window.onload = function() {

        // Check for LocalStorage support.
        if (localStorage) {

            // Add an event listener for form submissions
            document.getElementById('digitizer_form').addEventListener('submit', function() {

            // Get the value of the MedicineName field :

Now, this is where I need help. Here I want to give in the 'MedicineName' 
field of the form defined above, So that
I can get the MedicineName entered by the user from the form
and store it in the browser storage. 
The logic should look something like this:

            // get the value of the MedicineName so that
               I can store it in a variable like this:

             var Medicine = document.getElementById('MedicineName').value; 

//but because I am using a django form, and linking it 
  in the e_commerce.html template through {%csrf_token%} ,
  {{iform.as_table}} and {{ dform.as_p }} 
  and have not assigned any id ('MedicineName') to any element (as 
  I didn't create any form element manually inside the html template, 
but has made a "form class" in forms.py), I can't follow
  this approach and so I am stuck here because I don't know how to 
  do it the other way round.

            // and then Save the variable in localStorage
              like this :
            localStorage.setItem('Medicine', MedicineName);
            });

          }

        }
</script>

So, how to use the MedicineName entered by the user from the Django form inside the javascript in the html template, to store the name in my browser's local storage through javascript?那么,如何在html模板的javascript中使用用户从Django表单输入的MedicineName,通过javascript将名称存储在浏览器的本地存储中呢?

It would be greatly appreciated if anyone could help me with this.如果有人能帮我解决这个问题,我将不胜感激。 Thanks in advance!提前致谢!

There is an error in your form rendering您的表单呈现错误

<form id="digitizer_form" role="form" action="/MediciansDigitizer/Presc/" method="post" enctype="multipart/form-data">

   {% csrf_token %}

   <h3 class="prod_title">Add Detail</h3>

   {{iform.as_table}}

   <div class="">
   <button type="Submit" class="btn btn-default btn-lg">Digitize</button>
   </div>

</form> 

<form id="other_form" role="form" method="post" >

   {{ dform.as_p }}

   <div class="">
   <button type="Submit" class="btn btn-default btn-lg">Digitize</button>
   </div>

</form> 

These are two forms so they should be rendered as two different forms.这是两种形式,因此应将它们呈现为两种不同的形式。 Else you will find that server side form validation does not work.否则你会发现服务器端表单验证不起作用。 If you need one HTML form with fields from two django forms, have one django form extend the other.如果您需要一个包含来自两个 django 表单的字段的 HTML 表单,请让一个 django 表单扩展另一个。

Then since you have a form element named medicinename rendering the form will result in an HTML input element with an id of id_medicinename thus you can access it as然后,由于您有一个名为medicinename的表单元素,该表单将生成一个 id 为id_medicinename的 HTML 输入元素,因此您可以访问它

 document.getElementById('id_medicinename').value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django会话与浏览器本地存储 - Django Sessions Vs Browser Local Storage 如何在本地存储中存储表单数据? - How to store form data in local storage? 通过HTML5验证使用Javascript检查表单是否有效,并将表单数据存储在本地存储中 - Check using Javascript if form is valid by HTML5 validation and store form data in local storage 如何使用javascript中的LOGIN表单从本地存储获取数据? - How to get data from local storage using LOGIN form in javascript? JavaScript-使用HTML5本地存储来存储表单数据并将其显示在“谢谢”页面上 - JavaScript - Using HTML5 Local Storage to store form data and to present it on the “Thank You” page JavaScript-将表单文本存储在本地存储中 - JavaScript - Store Form Text in Local Storage 使用数组将表单数据存储在本地存储中并在新页面上检索 - Store form data in local storage using array and retrieving it on new page 如何将多个表单数据值存储到本地存储中的单个键 - How to store multiple form data values to a single key in local storage 将表单数据存储在本地存储中并显示在表格中 - Store form data in Local Storage and display in table 将多个表单输入值以数组的形式存储在 JavaScript 中的本地存储中 - Store multiple form input values in local storage in JavaScript in the form of an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM