[Answered ]-How to use Ajax in Django-templates?

2👍

This is how I handle it in Flask, but since you are working with Django it will be easy to fit the code.

Server:
import jsonify

@app.route("/_jquerylink")
def jquerylink():
    val = request.POST.get('val', default='text_text')
    return jsonify(text=val)

Client-side:

<div id="partwithh1"><h1>{{ text }}</h1></div>

<form action="/home/" method="post">
  {% csrf_token %}
  <input type="text" name="val" id="val" value="{{ val }}"/> <br/>
  <input type="submit" onclick="getviews(this)" value="OK" />
</form>

    <script type="text/javascript">

        $(function getviews() {

            var content = $(#val).text();
            $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
            $.getJSON($SCRIPT_ROOT + '/_jquerylink', 
                    {'val':content}
                ,
                function(data) {
                    $("#partwithh1").html(data.text);
                });
            return false;
            });

     </script>
👤Tasos

Leave a comment