Seungwoo
Seungwoo

Reputation: 3

How pass the variable from template to view?

template.html

<input type="text" id="text">
<input id="submit" name="submit"  type="submit">
<div id="roro"></div>

<script type="text/javascript">
$('#submit').click(function(){
    $.ajax({
        url:'',
        data : {'text':$('#submit').val()},
        type: 'POST',
        dataType : 'json',
        success:function(data){
            $('#roro').text(result['data']);
        },
        error:function(r){
            alert(text);
        }

   });
});
</script>

view.py

@csrf_exempt
def construction(request):
    if request.method == 'POST':
        text = request.POST['text']
        return render(request, 'pybo/construction_compare.html', text)
    return render(request, 'pybo/construction_compare.html')

It's a test for pass of variables. So if it's failed I want see variable on alert..

I found many of articles but I failed.. And It has many fault because I studying alone and My brain is not good :(

Upvotes: 0

Views: 113

Answers (2)

Sajere Kells
Sajere Kells

Reputation: 121

You should specify the url to the view function to the url in your script.

<script type="text/javascript">
    $('#submit').click(function(){
    $.ajax({
        url:"{% url 'the_url_to_view' %}",
        data : {'text':$('#submit').val()},
        type: 'POST',
        dataType : 'json',
        success:function(data){
        $('#roro').text(result['data']);
    },
    error:function(r){
        alert(text);
    }

  });
});
</script>

Upvotes: 0

Ada
Ada

Reputation: 49

you have to specify the url in your ajax request.

Upvotes: 1

Related Questions