[Fixed]-Issue with custom template tags in Django?

1👍

Templates are rendered by the server. Which means all the tags get processed on the server and a plain HTML page is then generated which is sent to the browser/client.

In short:

<button onClick="{% set n = True %}">x</button>

will be rendered to the browser/client as:

<button onClick="True">x</button>

UPDATE

Since, you’re trying to update the value of n.read asynchronously when a button is clicked, you’ll need to find a way to communicate to the server to update the value of n.read.

Below is a minimal example of performing an AJAX request to the server using jQuery library.

<button id="myBtn" data-id="{{ n.id }}">x</button>

<!-- JavaScript -->
<!-- Don't forget to include jQuery -->
<script>
$("#myBtn").on('click', function(), {
    var nId = $(this).attr('data-id');
    $.ajax({
        type: 'POST',
        url: '/link/to/the/view/',
        data: {'id': nId}, // this way server will know which object to update
    });
});
</script>

Now, an AJAX request will be made to the server. You’ll need to implement a view on the server which takes care of updating and saving the n object.


UPDATE 2

You can find a lot more examples on using Django and AJAX (jQuery) by searching StackOverflow. Well, you can start here.

👤xyres

Leave a comment