![](/img/trans.png)
[英]How to set variable inside django template with javascript and pass it to views.py?
[英]How do I pass my variable from template to views.py
我想知道如何將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")
你的表單方法應該是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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.