如何使用JQuery Datepicker与Django模板语言 [英] How to use a JQuery Datepicker with the Django template language
问题描述
查看此链接和找到一些帮助,但我想知道如何可以选择使用JQueryUI Datepicker小部件for DateField我在我的models.py
Looked at this link and found some help but I am wondering how I can choose to use a JQueryUI Datepicker widget for a DateField I have in my models.py
models.py
from django.db import models
class EModel(models.Model):
date = models.DateField(blank=False)
forms.py
from django import forms
from models import EModel
class EForm(forms.ModelForm):
class Meta:
model = EModel
form.html - Django如何呈现我的表单。 不在管理页面
form.html - How Django renders my form. Not in the admin page
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Form</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
</head>
<body>
<form action="" method="post">{% csrf_token %}
{{ form.date }} <!-- ***** -->
<!-- The rest of my form -->
</form>
</body>
我希望有一种方法可以使我的'date'模型字段渲染为JQueryUI Datepicker小部件,但是我已经搜索了,发现没有办法链接两个(在我的情况下)。
I am hoping for a way to make my 'date' model field render as a JQueryUI Datepicker widget, but I have searched around and found no way to link the two (in my case).
解决方案
在我的forms.py
In my forms.py
from django import forms
from models import EModel
class EForm(forms.ModelForm):
class Meta:
model = EModel
widgets = {
'date' : forms.DateInput(attrs={'type':'date'})
}
推荐答案
在你的模型窗体中编辑你的日期窗口小部件属性,给他们类'.datepicker'这就是JQuery datepicker将要查找的内容。所以你的ModelForm变成:
Edit your date widgets attributes in your model form, giving them the class '.datepicker', as that's what the JQuery datepicker will look for. So your ModelForm becomes:
class EForm(forms.ModelForm):
class Meta:
model = EModel
widgets = {'date': DateInput(attrs={'class': 'datepicker'})}
文档此处。
这篇关于如何使用JQuery Datepicker与Django模板语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!