我正在使用django.contrib.admin.widgets的FilteredSelectMultiple小部件来获取ModelMultipleChoiceField。这在我的django 1.8环境中效果很好,但是当我用1.10.1尝试它时,它无法在select之后呈现addEvent <script>
标记。
class UserCatForm(forms.ModelForm):
"""Form to select Categories for user"""
form_errors = {"required": "You must choose at least one expense category"}
cats = forms.ModelMultipleChoiceField(
error_messages=form_errors,
label="",
queryset=Categories.objects.all(),
widget=FilteredSelectMultiple("Categories", is_stacked=False, attrs={'class':'form-control'}),
)
class Meta:
name = 'UserCatForm'
model = UserCat
fields = "__all__"
widgets = {'user':forms.HiddenInput(),}
base.html文件
{% load staticfiles %}
<html>
<head>
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_messages %}
<link rel="stylesheet" href="{% static 'css/budget.css' %}">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="/static/admin/css/widgets.css" />
<script type="text/javascript" src="{% static 'js/jsi18n.js' %}"></script>
<script type="text/javascript" src="/static/admin/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="{% static 'admin/js/core.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/SelectBox.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/SelectFilter2.js' %}"></script>
<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>
<script>
$(function() {
$( "#id_nextDueDate" ).datepicker();
$( "#id_endDate" ).datepicker();
});
</script>
{% block title %}{% endblock %}
</head>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 well well-sm">
<h1 class="center">GreenPrint PayPlanner</h1>
<div class="row">
<div class="col-xs-6">
{% if user.is_authenticated %}
{% if user.get_full_name %}
<p>Welcome {{ user.first_name }}!
{% else %}
<p>Welcome {{ user }}!
{% endif %}
<a title="Manage Settings" href="{% url 'manage' %}"><img border="0" width="75" alt="Manage Account" src="{% static "pics/settings.png" %}"></a>
</p>
</div>
<div class="col-xs-6 text-right">
<a href="{% url 'logout' %}" class="text-warning">Logout</a>
</div>
{% endif %}
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">{% block content %}{% endblock %}</div>
</div>
<div class="row">
<div class="col-xs-12">{% block footer %}{% endblock %}</div>
</div>
</div>
</html>
manage.html - 带表单的模板
{% extends "base.html" %}
{% load bootstrap3 %}
{% block title %}
<title>Account Settings</title>
{% endblock %}
{% block content %}
<div class="container-fluid">
<div class="row">
<div class="col-m-3"></div>
<div class="col-m-9">
<form action="{% url 'manage' %}" method="post">
<div class="form-group">
{% csrf_token %}
{{ form.as_table }}
</div>
</div>
<div class="col-m-3"></div>
</div>
<div class="row center">
<div class="col-xs-12">
{% if is_get %}
<div class="center btn-group-vertical btn-group-lg">
<input type="submit" name="profile" value="Edit Profile" class="btn btn-primary"/>
<input type="submit" name="categories" value="Edit Categories" class="btn btn-primary"/>
<input type="submit" name="deldata" value="Delete Budget" class="btn btn-warning"/>
<input type="submit" name="delacct" value="Delete Account" class="btn btn-danger"/>
<input type="submit" name="home" value="Home" class="btn btn-default"/>
</div>
{% else %}
<div class="btn-group">
<input type="submit" name="save_{{ form.Meta.name }}" value="Save" class="btn btn-primary"/>
<input type="submit" name="cancel" value="Cancel" class="btn btn-default"/>
</div>
{% endif %}
</div>
</div>
</form>
</div>
{% endblock %}
{% block footer %}
<div class="center">{{ footer }}</div>
{% endblock %}
工作页面来源表单:
<tr><th></th><td><select multiple="multiple" class="selectfilter" id="id_cats" name="cats">
<option value="1" selected="selected">Other</option>
<option value="2" selected="selected">Education</option>
<option value="3" selected="selected">Utilities</option>
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_cats", "Categories", 0, "/static/admin/"); });</script>
<input id="id_user" name="user" type="hidden" value="1" /></td></tr>
不工作页面来源:
<tr><th></th><td><select multiple="multiple" class="selectfilter" data-field-name="Categories" data-is-stacked="0" id="id_cats" name="cats" required>
<option value="1" selected="selected">Other</option>
<option value="2" selected="selected">Utilities</option>
<option value="3" selected="selected">Education</option>
<option value="4" selected="selected">Car</option>
</select><input id="id_user" name="user" type="hidden" value="1" /></td></tr>
答案 0 :(得分:1)
我添加了将脚本直接放入我的模板中并立即运行。
<div class="form-group">
{% csrf_token %}
{{ form.as_table }}
<script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_cats", "Categories", 0, "/static/admin/"); });</script>
</div>
答案 1 :(得分:1)
我找到了更多django-esk解决这个问题的方法,想想如果有人遇到这个问题,我会添加它。这适用于1.8和1.10.1
我将addEvent调用放入外部js文件./static/js/catsfilter.js
Bob,Apples,16 ( for example )
在我的表单上创建了一个Media子类来调用js
addEvent(window, "load", function(e) {SelectFilter.init("id_cats", "Categories", 0, "/static/admin/"); });
删除了对core.js,SelectBox.js和SelectFilter2的调用,取而代之的是调用base.html head部分中的表单媒体
class Media:
js = ("js/catsfilter.js",)