How to use Django variable in Javascript

Submitted 3 years, 6 months ago
Ticket #182
Views 268
Language/Framework Django
Priority Medium
Status Closed

I have a requirement to read the 

{{ request.user.username }}

in Javascript to pass the value to Django backend. But looks like syntax doesnt allow me to pass the value. What would be the syntax for sending the Django variables from Javascript.

Submitted on Sep 29, 20
add a comment

1 Answer
1

1

Verified

You can submit form with some data:

<form action="POST">
<input type="hidden" name="name" value="value">
</form>

Or send request by ajax:

  $.ajax({
        method: "POST",
        // url: "some.php",
        data: {name: "value", variable: "value"}
      })
        .done(function( msg ) {
       // deal with msg
    }

Then in django view you can get value:

name = request.POST.get('name')

Submitted 3 years, 6 months ago


Latest Blogs