How do I pass a Django template variable to an external JS?

Source: Internet
Author: User

Django is PythonDevelopmentThe most commonly used technology in thehave been thinking about how to better organize lately.Djangostatic resources, such asJS,CSSOne , how do you write better code and structure with a front-end build tool? one of the first problems that needs to be addressed is the need toJsThe code is written in the template to get the variables passed in the background, such as: <Div><H1>Testh1><divid= "My-test">div>div><Script> $(function(){ $ (' #my-test '). html ("{{Some_var_from_view}}") });script>If you write code like this, it's awkward to say that the front-end building tools likeWebpack,Gulpwill greatly reduce the range of use. first of all to say the conclusion that want to completely peelJsand templates that need to be rendered using template data, I didn't think of any good way. If the reader has a good way to enlighten. since it cannot be completely stripped, then go into the greatest effort to separateJsThe data and code you need. now that you need the template rendering data toJsThe first way to think about it is to render the data toHTMLcode, and hide it. The advantage of this approach is that it is simple, and even the template can be completely unusedtags. The disadvantage is that it renders a lot of hidden fields,JS to write a lot ofgetelementsbyxxxxa class of code to get the data. That being the case , then using a compromise approach,Htmlused inThe label renders the data passed in the background asJS object, then you can use this object directly in JS code. For example, in a template: <Script>varMyviewvar = { Var_1: {{var_1}}, Var_2: {{var_2}}, };script> ... <ScriptType= "Text/javascript" src= "/js/test_script.js" >script>One thing to be aware of in this way is to write the rendering as first as possibleJsthe code for the variable, such as written inHeadand then introduce the externalJsfile. Thus, inTest_script.jscan be used directly in theMyviewvarthis object. of course, since it's popularSPA(Single Page application) site, thenDjangoonly available as a back-endJSONdata is a way of doing it, but it's a test of the ability of front-end personnel. Source: hi! roy!

How do I pass a Django template variable to an external JS?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.