Django模板中的Javascript

问题描述

我在Django模板中使用JS,但是JS代码没有响应。该怎么办?

<script>标记内:

 var pageStrucVal = {{ pagestruc }};
        if (pageStrucVal >= 82){
            var pageWarn = ["Brilliant,your site's structure and design is awesome and rocking. <gold>RockingStructure</gold>",'pp']
        }
            
        else if ( pageStrucVal >= 72){
            var pageWarn = ["Nice,your site's structure and design is suitable",'pp']
            }
            
        else if ( pageStrucVal < 45){
            pageWarn = ["Extremely bad condition,your site's structure and design is extremely bad and unmanaged. Strictly to use a different theme or change the interface (UI). <red>#ExtremelyBad</red>",'dd'];
            }
        else if ( pageStrucVal < 50){
            pageWarn = ["Worst conditon,the content quality of your site is too much poor. Use a well-managed theme or change the interface (UI).<orange>#ReallyPoor</orange>",'dd'];
            }
        
        else if ( pageStrucVal < 55){
            pageWarn = ["Too much bad,your site's structure and design is too much bad and unmanaged. Try to use a different theme or change the interface (UI).<orange>#VeryPoor",'dd'];
            }
        
        else if ( pageStrucVal < 63){
            pageWarn = ["Very bad,your site's structure and design is very unmanaged. You can try to use a different theme or change the interface (UI).<orange>#MuchBad</orange>",'dd'];
            }
        
        else if ( pageStrucVal < 72){
            pageWarn = ["Bad,your site's structure and design isn't suitable. Small change in the interface (UI) is required.<green> #TendsToBad</green>",'dd'];
            }
        console.log(pageWarn);
        document.getElementById(pageWarn[1]).appendChild(document.createElement('p')).innerHTML = (pageWarn[0]);
    

document.getElementById(pageWarn[1]).appendChild(document.createElement('p')).innerHTML = (pageWarn[0]);是否正确书写且位置正确?

这是HTML:

<div class='plusNdefects' id='moredetails'>
            <h1>Good Aspects and Plus Points.</h1>
            <p class='details'>Here are some awesome good aspects and benefits you have in your site to get Google Adsense.</p>
        
            <!-- Ads ::> -->
            {% include "ads/outads.html" %}
            <!-- Ads <:: -->
        
            <div class='persona'>
                <div id='pp'> </div>
            </div>
        </div>
        
        <div class='plusNdefects'>
            <h1>disabilities and Improvements.</h1>
            <p class='details'>These are some disabilities of your website that are stopping you from being approved by Google
                Adsense.</p>
        
            <!-- Ads ::> -->
            {% include "ads/outads.html" %}
            <!-- Ads <:: -->
        
            <div class='persona'>
                <div id='dd'></div>
            </div>

'pp'和'dd'是我想插入消息的ID(位置)。 “ pp”代表正点,“ dd”代表缺陷。

解决方法

尝试var pageStrucVal = parseInt({{ pagestruc }});

,

我认为django标签之外的用户qoutes是一个更好的主意。var pageStrucVal = parseInt('{{ pagestruc }}');