我到处搜索过,并尝试过针对同一问题的多种解决方案,但我无法弄清楚我在做什么错。我正在尝试在django表单中使用jquery datepicker小部件,但是当我单击该字段时,该小部件将不会加载。
我的base.html:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<!-- Jquery UI -->
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{% static 'student_input/main.css' %}">
<title>Student Data</title>
</head>
我的HTML:
{% extends "student_input/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<head>
<script
src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"
integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30="
crossorigin="anonymous"></script>
</head>
<h2>New Student</h2>
<script type="text/javascript">
$( function() {
$( "#id_input_date" ).datepicker();
});
</script>
<script type="text/javascript">
$(document).ready(
function(){
$("#jqtest").html("JQuery installed successfully!");
}
);
</script>
<p id="jqtest"></p>
<form method="post" class="post-form">{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="save btn btn-default">Save Student</button>
</form>
{% endblock content %}
forms.py:
from django import forms
from .models import Student
class StudentForm(forms.ModelForm):
class Meta:
model = Student
widgets = {'input_date': forms.DateInput(attrs={'class': 'datepicker'})}
fields = ('first_name',
'last_name',
'student_level',
'student_reading',
'student_dictation',
'student_speaking',
'student_writing',
'input_date',
'student_notes',
models.py:
from django.db import models
class Student(models.Model):
input_date = models.DateField(blank=False)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
student_level = models.CharField(max_length=15, choices=level_select)
student_reading = models.CharField(max_length=3, choices=number_select)
student_dictation = models.CharField(max_length=3, choices=number_select)
student_speaking = models.CharField(max_length=3, choices=number_select)
student_writing = models.CharField(max_length=3, choices=number_select)
student_notes = models.TextField()
def __str__(self):
return self.first_name
我能够从具有类似问题的人员那里收集到的所有信息都导致了我的代码的此迭代,但仍未显示该小部件。我尝试了各种不同的方式来加载jquery,包括下载文件并从本地磁盘加载它。这些都不能解决问题。任何帮助将不胜感激。我不确定该寻找什么了。