from django import forms
from django.contrib.admin.widgets import FilteredSelectMultiple
from myproject.customUsers.models import SchoolClass#,Student
from django.contrib.auth.models import User
from myproject.widgets import CalendarWidget
class ConsentFormTpl(forms.Form):
title = forms.CharField()
message = forms.CharField()
deadline = forms.DateField(widget=CalendarWidget)
availClass = forms.ModelChoiceField(queryset=SchoolClass.objects.all(),empty_label="None")
students = forms.ModelMultipleChoiceField(queryset=User.objects.filter(groups__name='Students'),widget=FilteredSelectMultiple("Students",is_stacked=False))
在我的小部件文件中,我有:
class CalendarWidget(forms.DateInput):
class Media:
css = {
'all':('css/ui-darkness/jquery-ui-1.8.9.custom.css',),
}
js = ('js/jquery-ui-1.8.9.custom.min.js',)
最后在我的模板中,我使用了{{form.media}}
。奇怪的是,CalendarWidget的css和js文件没有出现在我的html中。 (FilteredSelectMultiple的文件在那里)。我已经查看了它已经检查过的shell:
>>> from django import forms
>>> from myproject.widgets import CalendarWidget
>>> class C(forms.Form):
... title = forms.DateField(widget=CalendarWidget)
...
>>> c=C()
>>> print c.media
<link href="http://xyz.com/media/css/ui-darkness/jquery-ui-1.8.9.custom.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="http://xyz.com/media/js/jquery-ui-1.8.9.custom.min.js"></script>
模板:
{% extends "base.html" %}
{% block content %}
<script type="text/javascript">
$(document).ready(function(){
//some stuff here
})
}
//When user change classes, load new set of students
$("select#id_availClass").change(function(){
getStud( $(this).val() );
})
$("#id_deadline").datepicker();
// Loads initial set of students when form loads
getStud(-1);
})
</script>
<link rel="stylesheet" type="text/css" href="http://xyz.com/media/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="http://xyz.com/media/admin/css/forms.css" />
<script type="text/javascript" src="/admin/jsi18n/" />
<script type="text/javascript" src="/media/admin/js/core.js" />
{{ form.media }}
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="yes" value="Submit" />
</form> {% endblock %}
更新:我设法打印表单媒体,然后将其推入render_to_response。文件在那里!
form = ConsentFormTpl()
print form.media
return render_to_response('consent_form/consent_form2.html',{'form':form},context_instance=RequestContext(request))
有人指点吗?
答案 0 :(得分:1)
啊,感谢@seitaridis,我设法找出了我的错误。我没有指定doctype,并且`是不允许的。天哪,我为这样一个简单的错误感到尴尬。
答案 1 :(得分:0)
尝试将以下内容添加到自定义窗口小部件文件的顶部。
from django import forms
答案 2 :(得分:-1)
问题可能是因为您在CalendarWidget
关键字参数中提供了widget
类而不是实例吗?
尝试更改:
deadline = forms.DateField(widget=CalendarWidget)
到
deadline = forms.DateField(widget=CalendarWidget())