Skip to content

Commit 15b317e

Browse files
committed
Check if CSRFProtect is enabled when using table delete action
1 parent 0eff73c commit 15b317e

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

docs/macros.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,21 @@ The following arguments are expect to accpet an URL tuple:
520520
You can also pass a fiexd URL string, but use a primary key placeholder in the URL is deprecated and will be removed
521521
in version 2.0.
522522

523-
The ``new_url`` expects a fixed URL string or an endpoint.
523+
When setting the ``delete_url``, you will also need to enable the CSRFProtect extension provided by Flask-WTF, so that
524+
the CSRF protection can be added to the delete button:
525+
526+
.. code-block:: text
527+
528+
$ pip install flask-wtf
529+
530+
.. code-block:: python
531+
532+
from flask_wtf import CSRFProtect
533+
534+
csrf = CSRFProtect(app)
535+
536+
By default, it will enable the CSRF token check for all the POST requests, read more about this extension in its
537+
`documentation <https://flask-wtf.readthedocs.io/en/0.15.x/csrf/>`_.
524538

525539

526540
render_icon()

flask_bootstrap/templates/bootstrap/table.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% from 'bootstrap/utils.html' import render_icon, arg_url_for %}
22

33
{% macro deprecate_placeholder_url() %}
4-
{{ warn('Passing an URL with primary key palceholder for view_url/edit_url/delete_url/custom_actions is deprecated. You will need to pass an fixed URL or an URL tuple to URL arguments, see the API docs of render_table for more details. The support to URL string will be removed in version 2.0.') }}
4+
{{ warn('Passing an URL with primary key palceholder for view_url/edit_url/delete_url/custom_actions is deprecated. You will need to pass an fixed URL or an URL tuple to URL arguments, see the docs for more details (https://bootstrap-flask.readthedocs.io/en/stable/macros.html#render-table). The support to URL string will be removed in version 2.0.') }}
55
{% endmacro %}
66

77
{% macro build_url(endpoint, model, pk, url_tuples) %}
@@ -147,6 +147,9 @@
147147
action="{{ build_url(delete_url[0], model, row[primary_key], delete_url[1]) | trim }}"
148148
{% endif %}
149149
method="post">
150+
{% if csrf_token is undefined %}
151+
{{ raise('You have to enable the CSRFProtect extension from Flask-WTF to use delete_url, see the docs for more details (https://bootstrap-flask.readthedocs.io/en/stable/macros.html#render-table).') }}
152+
{% endif %}
150153
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
151154
<a class="action-icon text-decoration-none"
152155
href="javascript:{}"

0 commit comments

Comments
 (0)