0

I want to add custom JavaScript everytime as per the logic in my backend.

For example:

--views.py-- ...

js="JavaScript which i want to add"
js_={'js_script':js}
return render(request,'html.html',context=js_)

--html.html-- ....

<script>
{{js_script}}

</script>


But this does not properly work and a weird &quot is added in various places in the html source code everytime and the work is not accomplished. Please Help if you have a work around.

2 Answers 2

1

Just tried something and it worked.

source: django docs - safe templatetag

in context you are doing it right example:

context = {
    'js_var': 'console.log("JavaScript which i want to add")',
}

in template add template tag "safe" it will remove the "&quot" 's:

<script>
console.log('sanity check');
 {{js_var|safe}}
 
</script>

output:

sanity check
JavaScript which i want to add

Have a good day!

Tell me if this worked for you!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot . It Works 200% as per my need. Thanks a lot for Helping me.
0

i think you want to load custom javascript code locally for particular page, to do that the proper way without mixing things, you need to define {% block %} in your base.html template and via DTL inheritance mechanism you can load you javascript the right way in right order without any conflict

in base.html define, let say {% block javascripts_local %}{% endblock %} like

{% load static %}
<!doctype html>
<html class="no-js" lang="{% block lang %}en{% endblock %}">
<head>

[..]

</head>
<body{% block body_attributes %}{% endblock %}>

[..]

  {% block javascripts %}

  <!-- i'm using HTML5 Boiler Plate template -->
  <script src="{% static 'js/vendor/modernizr-3.7.1.min.js' %}"></script>
  <script src="{% static 'js/plugins.js' %}"></script>
  <script src="{% static 'js/main.js' %}"></script>

  <!-- override this block in child template -->
  {% block javascripts_local %}{% endblock %}

  {% endblock %}
</body>
</html>

and then in your child template override the block

{% extends 'base.html' %}
{% load static %}


[..]

{% block javascripts_local %}
<script>
  // Your javascript code goes here
</script>
{% endblock %}

3 Comments

Thanks a lot for your Help. But I asked something else, but still thanks a lot it will help me in some other work to make the website more efficient thank you.
yes you're right, but it worth to mention that views functions are not meant to generate javascript code they are meant for something else for e.g: processing data loaded from csv file, requesting databases .. and then pass the results to templates. just good practices.
Yes You are Absolutely Right, I just have a need for it this time for a specific task i.e I have some javascript frameworks in use in the frontend which needs specific results as inputs from the backend(specifically Vector Coordinates for Plotting a Graph). But otherwise I Absolutely agree with your Point, and will always keep it in mind to not do such things like generating js code from backend unless it is absolutely neccessary. Also Generating Js code from backend is very much troublesome for me and it is definitely worth it to avoid it as far as possible. Thank You Once Again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.