Last active
October 10, 2020 00:56
-
-
Save andybak/11322035 to your computer and use it in GitHub Desktop.
Example of adding Django CSRF token in an page that POSTs using intercooler.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "custom_base.html" %} | |
{% block content %} | |
<form id="defaults" > | |
<input name="csrfmiddlewaretoken" type="hidden" value="{{ csrf_token }}"> | |
</form> | |
<table> | |
{% for row in items %} | |
<tr> | |
<td>{{ row.item }}</td> | |
<td>{{ row.category }}</td> | |
<td><button ic-include="#defaults" ic-post-to="/api/{{ row.row_type }}/{{ row.id }}/action1/">Action 1</button></td> | |
<td><button ic-include="#defaults" ic-post-to="/api/{{ row.row_type }}/{{ row.id }}/action2/">Action 2</button></td> | |
<td><button ic-include="#defaults" ic-post-to="/api/{{ row.row_type }}/{{ row.id }}/action3/">Action 3</button></td> | |
</tr> | |
{% endfor %} | |
</table> | |
{% endblock %} | |
{% block scripts %} | |
{{ block.super }} | |
<script src="{% static 'js/vendor/intercooler-0.2.0.min.js' %}"></script> | |
{% endblock %} |
Thanks Andy. This saved me time too!
Rather than adding ic-include
on every element, you could also add an ic-global-include
on the csrf input element.
e.g. :
<input name="csrfmiddlewaretoken" type="hidden" value="{{ csrf_token }}" id="csrf" ic-global-include="#csrf">
Docs: https://intercoolerjs.org/attributes/ic-global-include.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you from the future - IntercoolerJS should totally add this on their site..