如何将变量从模板传递到views.py

时间:2019-04-09 18:33:35

标签: javascript django

我想知道如何将oobcode值传递给views.py中的def postresetusername。

   reset_username.html  

   <script type="text/javascript">
   var oobcode;
   function func(){
       oobcode = localStorage.getItem("storageName");
   }
   </script>

  views.py

  def postresetusername(request):
    authe.verify_password_reset_code(oobcode,"new_password")
    return render(request, "reset_username.html")

1 个答案:

答案 0 :(得分:0)

您的表单方法应为method = "post"action = "the url for the view"

JQUERY

<script>
$(function(){
            var frm = $('#yourform');

            frm.submit(function (e) {
                var oobcode = localStorage.getItem("storageName");
                e.preventDefault();

                    $.ajax({
                        type: frm.attr('method'),
                        url: frm.attr('action'),
                        data:
                        frm.serialize()+ "&oobcode="+oobcode+"",

            success: function (data) {
                console.log('Submission was successful.');
            },
            error: function (data) {
                console.log('An error occurred.');
                console.log(data);

            },
                });    
            });
        });
        </script>

VIEWS.PY

  def postresetusername(request):
    oobcode = request.POST.get('oobcode',False)
    authe.verify_password_reset_code(oobcode,"new_password")
    return render(request, "reset_username.html")