I'm currently working on developing an app using Django and I've encountered a strange issue.
In my template, I have the following code:
<div class="form-group">
<input name="Data_inserimento_entry" type="date" class="form-control" id="date_to_turn_into_toda" >
</div>
<script type="text/javascript" src={% static "js/get_today_date.js" %}></script>
<script> get_today_date(date_to_turn_into_toda) </script>
In the JavaScript file get_today_date.js
located at static > js > get_today_date.js, the function is as follows:
function get_today_date(id_data) {
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear()+"-"+(month)+"-"+(day);
document.getElementById(id_data).value = today;
}
When I run the server and load the template, today's date appears in the input field which is expected. Everything seems to work fine.
However, when I make changes to the ID attribute of the input element or modify the function name in both the script tags, such as:
<script type="text/javascript" src={% static "js/get_today_date.js" %}></script>
<script> get_today_date_ID(date_to_turn_into_today) </script>
And
function get_today_date_ID(id_data) {
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear()+"-"+(month)+"-"+(day);
document.getElementById(id_data).value = today;
}
The functionality stops working. I'm trying to figure out why this happens. Is there something wrong with how I'm calling the JavaScript functions?
Could it be that changing the names of the functions is causing issues in another part of the template or JavaScript file?
Note: Both the function in the JavaScript file and the script itself have identical names except for the file extension (.js). Could this potentially be causing the problem?
Update:
In my model, I've defined the following:
class mymodel(models.Model):
Data_inserimento_entry = models.DateField(blank=False, null=False, default=timezone.now().date() )
Update:
Here is the complete aggiungi_terminologia.html template in response to a request for further clarification:
<!-- Entire HTML content -->