1414=====
1515
1616Use this to insert a script tag via ``forms.Media `` containing additional
17- attributes (such as ``id `` and ``data-* `` for CSP-compatible data
17+ attributes (such as ``id ``, `` nonce `` for CSP support, and ``data-* `` for CSP-compatible data
1818injection.):
1919
2020.. code-block :: python
@@ -25,6 +25,7 @@ injection.):
2525 JS(" asset.js" , {
2626 " id" : " asset-script" ,
2727 " data-answer" : " 42" ,
28+ " nonce" : " {{ request.csp_nonce }} " , # For CSP support
2829 }),
2930 ])
3031
@@ -34,7 +35,7 @@ now contain a script tag as follows, without line breaks:
3435.. code-block :: html
3536
3637 <script type =" text/javascript" src =" /static/asset.js"
37- data-answer =" 42" id =" asset-script" ></script >
38+ data-answer =" 42" id =" asset-script" nonce = " random-nonce-value " ></script >
3839
3940The attributes are automatically escaped. The data attributes may now be
4041accessed inside ``asset.js ``:
@@ -65,21 +66,24 @@ So, you can add everything at once:
6566
6667 from js_asset import CSS , JS , JSON
6768
69+ # Get the CSP nonce from the request context
70+ nonce = request.csp_nonce
71+
6872 forms.Media(js = [
69- JSON({" configuration" : 42 }, id = " widget-configuration" ),
70- CSS(" widget/style.css" ),
71- CSS(" p{color: red;} " , inline = True ),
72- JS(" widget/script.js" , {" type" : " module" }),
73+ JSON({" configuration" : 42 }, id = " widget-configuration" , attrs = { " nonce " : nonce} ),
74+ CSS(" widget/style.css" , attrs = { " nonce " : nonce} ),
75+ CSS(" p{color: red;} " , inline = True , attrs = { " nonce " : nonce} ),
76+ JS(" widget/script.js" , {" type" : " module" , " nonce " : nonce }),
7377 ])
7478
7579 This produces:
7680
7781.. code-block :: html
7882
79- <script id =" widget-configuration" type =" application/json" >{" configuration" : 42 } </script >
80- <link href =" /static/widget/style.css" media =" all" rel =" stylesheet" >
81- <style media =" all" >p {color :red ;} </style >
82- <script src =" /static/widget/script.js" type =" module" ></script >
83+ <script id =" widget-configuration" type =" application/json" nonce = " random-nonce-value " >{" configuration" : 42 } </script >
84+ <link href =" /static/widget/style.css" media =" all" rel =" stylesheet" nonce = " random-nonce-value " >
85+ <style media =" all" nonce = " random-nonce-value " >p {color :red ;} </style >
86+ <script src =" /static/widget/script.js" type =" module" nonce = " random-nonce-value " ></script >
8387
8488
8589
@@ -152,10 +156,15 @@ widget classes for the admin than for the rest of your site.
152156
153157.. code-block :: python
154158
155- # Example for adding a code.js JavaScript *module*
159+ # Example for adding a code.js JavaScript *module* with CSP support
160+ nonce = request.csp_nonce
161+
162+ # Create importmap with CSP nonce
163+ importmap_with_nonce = ImportMap(importmap._importmap, {" nonce" : nonce})
164+
156165 forms.Media(js = [
157- importmap , # See paragraph above!
158- JS(" code.js" , {" type" : " module" }),
166+ importmap_with_nonce , # See paragraph above!
167+ JS(" code.js" , {" type" : " module" , " nonce " : nonce }),
159168 ])
160169
161170 The code in ``code.js `` can now use a JavaScript import to import assets from
0 commit comments