Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript {%csrf_token%}从js文件django打印为文本_Javascript_Html_Django_Csrf Token - Fatal编程技术网

Javascript {%csrf_token%}从js文件django打印为文本

Javascript {%csrf_token%}从js文件django打印为文本,javascript,html,django,csrf-token,Javascript,Html,Django,Csrf Token,{%csrf_token%}在Django中以文本形式从js文件打印如果在脚本标记中使用代码,则代码可以工作,但当我尝试从外部js文件使用它时,它不工作 HTML <div class="build">BUILD YOUR RESUME</div> <div class="box container" id="box2"> <div class="skillbox"

{%csrf_token%}
在Django中以文本形式从js文件打印如果在脚本标记中使用代码,则代码可以工作,但当我尝试从外部js文件使用它时,它不工作

HTML

<div class="build">BUILD  YOUR  RESUME</div>
<div class="box container" id="box2">
    <div class="skillbox">
        <div class="workquestion">
            How Many work Exprience you wanted <br> to add in your resume ?
        </div>
        <div class="row" id="butt">
          <div class="form-group col-md-2">
              <button type="button" class="btn btn-success" onclick="updateform(1)" id="butt1">1</button>
          </div>
          <div class="form-group col-md-2">
              <button type="button" class="btn btn-success" onclick="updateform(2)" id="butt2">2</button>
          </div>
          <div class="form-group col-md-2">
              <button type="button" class="btn btn-success" onclick="updateform(3)" id="butt3">3</button>
          </div>
          <div class="form-group col-md-2">
              <button type="button" class="btn btn-success" onclick="updateform(4)" id="butt4">4</button>
          </div>
          <div class="form-group col-md-2">
            <button type="button" class="btn btn-success" onclick="updateform(5)" id="butt5">5</button>
        </div>
        <div class="workquestion" style="color: red;">
            Maximum
        </div>
        </div>
        <div class="row" >
            <div class="form-group col-md-2">
                <button type="button" class="btn btn-success" onclick="updateform(6)" id="butt6">6</button>
            </div>
            <div class="form-group col-md-2">
                <button type="button" class="btn btn-success" onclick="updateform(7)" id="butt7">7</button>
            </div>
            <div class="form-group col-md-2">
                <button type="button" class="btn btn-success" onclick="updateform(8)" id="butt8">8</button>
            </div>
            <div class="form-group col-md-2">
                <button type="button" class="btn btn-success" onclick="updateform(9)" id="butt9">9</button>
            </div>
            <div class="form-group col-md-2">
              <button type="button" class="btn btn-success" onclick="updateform(10)" id="butt10">10</button>
          </div>
          <div class="workquestion" style="color: red;">
            10 Skills
        </div>
          </div>
</div>
建立你的简历
您希望在简历中添加多少工作经验?
1.
2.
3.
4.
5.
最大限度
6.
7.
8.
9
10
10项技能
脚本

<script src='{% static "js/skill.js" %}'>
     </script>

skill.js

function updateform(n){
    var temp=`<div class="heading">
  <h2 class="head">Tell us about your Skills</h2>
<form method="POST" action="/skill">{% csrf_token %}`
for(var i=1;i<=n;i++){
    temp=temp+`<div class="form-row">
        <div class="form-group col-md-3">
      </div>
      <div class="form-group col-md-6">
        <label for="inputEmail4">Skill ${i}</label>
        <input type="text" class="form-control" id="skill${i}" name="skill${i}" placeholder="Skill" required>
      </div>
    </div>`
}
temp=temp+`<button id='b1' type="submit" class="btn btn-primary nextbtn">Next</button>
  </form>
</div>`
document.getElementById('box2').innerHTML=temp
}
函数更新表单(n){
变温=`
告诉我们你的技能
{%csrf_令牌%}`

对于(var i=1;i您是否将JS文件作为模板在Django中处理


您需要引用Django URL,该URL将文件作为模板处理,然后返回处理后的内容(源文本…),而不是在脚本中的指定位置直接引用资源给您。

.js
文件按原样提供,而不是像HTML模板/视图那样进行预处理。将服务器数据传递给客户端有多种解决方案:在HTML中放入内联脚本或通过ajax请求数据是其中的两种。这是否回答了您的问题?是的,它在脚本标记中工作。您能否提供更多有关此问题的详细信息Ajax方法您不能将django模板语法放入
.js
文件中。您应该获取CSRF cookie值&如果需要让js这样做,就这样做。