在Django中包含自定义JavaScript的最佳实践?

时间:2016-10-27 18:56:24

标签: javascript django

Django还是新手。我正在尝试将日历窗口小部件合并到date输入中,如此tutorial所示。以下 test.html 可以正常运行:

{% extends "base_alt_simple.html" %} # note this has {% load static %}

{% block title %}Schedule{% endblock %}

{% block main_content %}

<div class="container">

  <h3>Building name</h3>

</div>

<div class="container">
<div class="bootstrap-iso">
 <div class="container-fluid">
  <div class="row">
   <div class="col-md-6 col-sm-6 col-xs-12">
    <form method="post">
     <div class="form-group ">
      <label class="control-label " for="date">
       Date
      </label>
      <div class="input-group">
       <div class="input-group-addon">
        <i class="fa fa-calendar">
        </i>
       </div>
       <input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="text"/>
      </div>
     </div>
     <div class="form-group">
      <div>
       <button class="btn btn-primary " name="submit" type="submit">
        Submit
       </button>
      </div>
     </div>
    </form>
   </div>
  </div>
 </div>
</div>
</div>
<script>
    $(document).ready(function(){
        var date_input=$('input[name="date"]'); //our date input has the name "date"
        var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
        date_input.datepicker({
            format: 'mm/dd/yyyy',
            container: container,
            todayHighlight: true,
            autoclose: true,
        })
    })
</script>

但是,如果我想将javascript保留在 custom.js 中,我该如何加载?

使用我使用的初始答案:

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

但日历弹出不再有效。我在这里明显误解了如何引入custom.js功能。谢谢你的任何建议。

此外, base_alt_simple.html

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>{% block title %}Home{% endblock title %}</title>

    <!-- Bootstrap core CSS -->
    <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="{% static 'css/font-awesome.min.css' %}" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Lora" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Abril+Fatface" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="{% static 'css/grayscale.min.css' %}" rel="stylesheet">
    <link href="{% static 'css/custom.css' %}" rel="stylesheet">

    <!-- favicon -->
    <link rel="shortcut icon" href="{%  static 'images/favicon.ico' %}">



    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">

<!-- Static navbar -->
<nav class="navbar navbar-default navbar-static-top" style="background-color: #FFFFFF;">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand page-scroll" href="{% url 'index' %}">
        <img id="logo" src="{% static 'images/Logo1.png' %}">
      </a>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <!-- <ul class="nav navbar-nav navbar-right"> -->
      <form method='GET' action='' class="nav navbar-nav navbar-right">
        <input type='text' name='q' placeholder='Search apartments' value='{{ request.GET.q }}'/>
        <input type='submit' value='Search' />
      </form>
        <!-- <li><a href="#">User <span class="glyphicon glyphicon-user" id="logIcon"></span></a></li> -->
        <!-- <li><a href="#"> <span class="glyphicon glyphicon-home" id="logIcon"></span></a></li> -->
      <!-- </ul> -->
    </div><!--/.nav-collapse -->
  </div>
</nav>

{% block main_content %}

{% endblock %}


    <!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="{% static 'bootstrap.min.js' %}"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="{% static 'ie10-viewport-bug-workaround.js' %}"></script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

<!-- Include jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

<!-- Bootstrap Date-Picker Plugin -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>





</body>



</html>

修改 添加了当前的完整 test.html

{% extends "base_alt_simple.html" %}
{% load static %}

{% block title %}Schedule{% endblock %}

{% block main_content %}

<div class="container">

  <h3>Building name</h3>

</div>

<div class="container">

  <div class="jumbotron">
  <h4>Find a time to visit </h4>

  <div class="container">
  <div class="row">


   <div class="col-md-6 col-sm-6 col-xs-12">
    <form method="post">
     <div class="form-group ">
      <label class="control-label " for="date">
       Date
      </label>
      <div class="input-group">
       <div class="input-group-addon">
        <i class="fa fa-calendar">
        </i>
       </div>
       <input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="text"/>
      </div>
     </div>
     <div class="form-group">
      <div>
       <button class="btn btn-primary " name="submit" type="submit">
        Submit
       </button>
      </div>
     </div>
    </form>
   </div>

  </div> <!-- row -->
  </div> <!-- /container -->

  </div> <!-- end jumbotron -->

</div> <!-- end container -->


{% endblock main_content %}


{% block scripts %}

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

{% endblock scripts %}

2 个答案:

答案 0 :(得分:0)

您必须使用静态文件,请按照this django doc中的步骤操作。

假设您将{{ old('search')}} 放在django app目录yourscript.js中,您只需执行以下操作:

your_app/static/yourscript.js

希望这有帮助!

<强>更新

确保在 bootstrap和datepicker之后放置 {em},这样你就可以放

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

custom.js <script src="{% static 'custom.js' %}"></script> 之前。或者如果你不想把它放在那里,你可以定义一个脚本块:

</body>

再次在base_alt_simple.html{% block scripts %} {% endblock %} 中的</body>之前:

base_alt_simple.html

另外,请勿忘记将test.html放入{% load static %} {% block scripts %} <script src="{% static 'custom.js' %}"></script> {% endblock %} 并重新启动服务器。

答案 1 :(得分:0)

Django以完全不同的方式将静态文件加载到模板中。您应首先设置static目录,然后使用Django的特殊模板标签从中加载静态文件。如果您在yourscript.js中有js代码,则在模板中执行以下操作:

{% load static %}

<script src="{% static 'relative/path/from/static/dir/yourscript.js' %}"></script>

Django将静态文件收集到static目录。要获得有关如何在项目中设置静态目录的更多信息,请阅读the official Django doc on static files.