Jebediah15 Jebediah15 - 1 month ago 9
Javascript Question

Best practice for including custom javascript in Django?

Still new to Django. I am trying to incorporate a calendar widget into

date
input as shown in this tutorial. The following test.html works fine:

{% 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>


But, if I would like to keep the javascript in a custom.js, how would I load this?

Using the initial answers I use:

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


But the calendar pop-out not longer works. I am clearly misunderstanding something here about how to pull in the
custom.js
functionality. Thanks for any advice.

Also, 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>


EDIT
added full current 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 %}

Answer

You have to use static files, follow the steps in this django doc.

Say you placed your yourscript.js in your django app directory your_app/static/yourscript.js, all you have to do is the following:

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

Hope this helps!

UPDATE:

Make sure custom.js is placed after bootstrap and datepicker, so you could put

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

right before </body> in base_alt_simple.html. Or if you don't want to put it there, you could define a scripts block:

{% block scripts %}

{% endblock %}

again right before </body> in base_alt_simple.html and in test.html:

{% load static %}
{% block scripts %}
<script src="{% static 'custom.js' %}"></script>
{% endblock %}

Also, don't forget to place custom.js in your_app/static/custom.js and to restart your server.