javascript - 如何将日期/日历小部件添加到 django 表单?
问题描述
我尝试关注这些链接,但这没有用。该小部件未在表单中加载,并且 js 文件在控制台中给出 404 错误,而它可以从链接访问。我使用酥脆的表格来呈现我的表格。
表格.py
DateInput = partial(forms.DateInput, {'class': 'datepicker'})
# patient Form
class PatientForm(ModelForm):
birth_date = forms.DateField(label='Birth Date',widget=DateInput())
class Meta:
model = Patient
exclude = ['user',]
模板
<h2>Add Patient</h2>
<form method="POST" action="{% url 'patients:patient_create' %}">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary" >Add Patients</button>
<p></p>
</form>
</div>
{% endblock %}
<script>
$(document).ready(function() {
$('.datepicker').datepicker();
});
</script>
js文件+CSS的链接
<script src=”https://code.jquery.com/jquery-1.12.4.js”></script>
<script src=”https://code.jquery.com/ui/1.12.1/jquery-ui.js”></script>
<link rel=”stylesheet” href=”//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css”>
控制台中的错误
Not Found: /patients/”https://code.jquery.com/jquery-1.12.4.js”
Not Found: /patients/”https://code.jquery.com/ui/1.12.1/jquery-ui.js”
[18/Jul/2019 15:29:09] "GET /patients/%E2%80%9Dhttps://code.jquery.com/jquery-1.12.4.js%E2%80%9D HTTP/1.1" 404 6742
[18/Jul/2019 15:29:09] "GET /patients/%E2%80%9Dhttps://code.jquery.com/ui/1.12.1/jquery-ui.js%E2%80%9D HTTP/1.1" 404 6760
Not Found: /patients/”https://code.jquery.com/jquery-1.12.4.js”
[18/Jul/2019 15:29:10] "GET /patients/%E2%80%9Dhttps://code.jquery.com/jquery-1.12.4.js%E2%80%9D HTTP/1.1" 404 6742
Not Found: /patients/”https://code.jquery.com/ui/1.12.1/jquery-ui.js”
[18/Jul/2019 15:29:10] "GET /patients/%E2%80%9Dhttps://code.jquery.com/ui/1.12.1/jquery-ui.js%E2%80%9D HTTP/1.1" 404 6760
Not Found: /favicon.ico
在我的代码中,我有很多 jquery 版本会导致问题。我想要一个比 jquery ui 更用户友好的小部件,那么有没有其他方法可以用来添加与 jquery ui 相同的小部件?
解决方案
在您的 forms.py 中:
class DateInput(forms.DateInput):
input_type = 'date'
class PatientForm(ModelForm):
class Meta:
model = Patient
exclude = ['user',]
widgets = {
'birth_date':DateInput,
}
尝试这个
推荐阅读
- python - CardTransition 模式有问题吗?
- php - 如何在没有边框的php中向PNG文件添加透明矩形
- python - 标签和绘图重叠
- python - django,谁从模型中获取过多数据并将其用作模板中的条件
- python - 如何使用 crontab 调度带有参数的 python 脚本?
- javascript - 如何在不刷新页面的情况下更新 JSP 文件中的输出?
- macos - 无法升级纱线(macOS 10.13.6)
- tfs - 有没有办法在不包含任何标签的情况下找到所有项目(产品积压项目、错误、任务)?(带查询)
- android - 如果倒数计时器完成且手机处于睡眠状态,应用程序会切换活动或离开应用程序
- php - 需要制作表格而不是div结构