Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate Javascript with custom tags in Django template
    primarykey
    data
    text
    <p>my problem is a bit unusual. In my project I have a static js file with many functions defined there. Then in each template I have a script tag where I define the necessary variables for this templates and call the function i need from the static file. And the view does nothing but rendering the template. So my template looks for example like :</p> <pre><code>{% extends "base.html" %} {% load static %} &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;/head&gt; &lt;body&gt; {% block content %} &lt;script type="text/javascript"&gt; $(document).ready(function(){ var first_var = 'foo'; var second_var ='bar'; my_function(first_var,second_var); }) &lt;/script&gt; &lt;div class="values"&gt;&lt;/div&gt; {% endblock %} &lt;/body&gt; </code></pre> <p></p> <p>And the view simply looks like : </p> <pre><code>def my_view(request): return render(request, 'my_app/my_template.html') </code></pre> <p>What I have to do, is instead of having to manually write the script tag in the template, have a custom template tag to which you can pass the variables needed and that will return the corresponding script. The reason i have to do that is to allow people who will use the app to avoid writing script and only wirte something like : </p> <pre><code> {% my_function 'foo' as first_var %} </code></pre> <p>I don't really know how to do this or if it's the right way to do so. The main point is using custom tags instead of wiritng the script, so how do you think this should be done ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload