Skip to content

Instantly share code, notes, and snippets.

@andybak
Last active October 10, 2020 00:56
Show Gist options
  • Save andybak/11322035 to your computer and use it in GitHub Desktop.
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.
{% 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 %}
@pebreo
Copy link

pebreo commented Jan 16, 2017

Thanks for posting. This saved me a lot of time!

@w-
Copy link

w- commented Jan 11, 2018

also thank you.

@ajangmoses
Copy link

thanks man

@hugows
Copy link

hugows commented May 24, 2019

Thank you from the future - IntercoolerJS should totally add this on their site..

@a-toms
Copy link

a-toms commented Jun 11, 2019

Thanks Andy. This saved me time too!

@pmbaumgartner
Copy link

pmbaumgartner commented Sep 24, 2019

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