我尝试遵循这些链接和这个,但没有成功。小部件不会在表单中加载,并且 js 文件在控制台中给出 404 错误,而可以通过链接访问它。我使用脆皮表格来渲染我的表格。
forms.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,
}
试试这个
问题是,如果不将
type
更改为 date
,现代浏览器将不会显示选择日期
这就是为我解决的问题
DateInput = partial(forms.DateInput, {'class': 'datepicker','type': 'date'})
# patient Form
class PatientForm(ModelForm):
birth_date = forms.DateField(label='Birth Date',widget=DateInput())
class Meta:
model = Patient
exclude = ['user',]
您要添加的是小部件属性字典中的 type 属性。像这样。 小部件 = forms.DateInput( 属性:{ '类型':'日期', '占位符':'你的-ph', })