使用ajax请求的django框架的链式选择框小部件。
django-clever-selects的Python项目详细描述
这个库的灵感来自于s-block (https://github.com/s-block/django-chained-selectbox)。
它使用ajax请求将选择框链接在一起,为django框架提供链式选择框小部件。 值根据父值而变化。
前面提到的库仅用于django管理。新库具有前端功能, 改进现有的实例数据初始化和新的^ {TT1} $。它还使用自定义的testclient 如果用户已登录,则可以将request.user变量传递到ajax视图中。如果您需要根据 例如,用户权限。
在Django 1.4.5测试。
要求
- Django
- jquery
安装
- 使用pip安装python库:pip install django clever selects
- 将clever_selects添加到django设置文件中的INSTALLED_APPS。
- 在{% load %}语句中添加clever_selects_extras,并在关闭</body>元素之前放置{% clever_selects_js_import %}标记。在正文内容之后加载clever-selects.js文件很重要,因此不要将其放入<;head>;<;/head>;!
用法
表格
表单必须继承自ChainedChoicesMixin(或ChainedChoicesForm/ChainedChoicesModelForm,具体取决于您的需要) 当已有实例或初始数据时加载选项:
from clever_selects.form_fields import ChainedChoiceField from clever_selects.forms import ChainedChoicesForm class SimpleChainForm(ChainedChoicesForm): first_field = ChoiceField(choices=(('', '------------'), (1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5'), )) second_field = ChainedChoiceField(parent_field='first_field', ajax_url=reverse_lazy('ajax_chained_view')) class MultipleChainForm(ChainedChoicesForm): first_field = ChoiceField(choices=(('', '------------'), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), )) second_field = ChainedChoiceField(parent_field='first_field', ajax_url=reverse_lazy('ajax_chained_view')) third_field = ChainedChoiceField(parent_field='second_field', ajax_url=reverse_lazy('ajax_chained_view')) fourth_field = ChainedChoiceField(parent_field='third_field', ajax_url=reverse_lazy('ajax_chained_view')) fifth_field = ChainedChoiceField(parent_field='fourth_field', ajax_url=reverse_lazy('ajax_chained_view')) class ModelChainForm(ChainedChoicesModelForm): brand = forms.ModelChoiceField(queryset=CarBrand.objects.all(), required=True, empty_label=_(u'Select a car brand')) model = ChainedModelChoiceField(parent_field='brand', ajax_url=reverse_lazy('ajax_chained_models'), empty_label=_(u'Select a car model'), model=BrandModel, required=True) engine = forms.ChoiceField(choices=([('', _('All engine types'))] + Car.ENGINES), required=False) color = ChainedChoiceField(parent_field='model', ajax_url=reverse_lazy('ajax_chained_colors'), empty_label=_(u'Select a car model'), required=False) class Meta: model = Car
注意,每个字段的ajaxurl可能因不同的目的而不同。有关更多用例,请参见示例项目。
为了预先填充子字段,表单可能需要对当前用户具有访问权限。这可以通过 窗体视图中窗体的初始化方法的用户。CaleNeDeltFrimeVIEW MIXIN需要注意 给你的。
class CreateCarView(ChainedSelectFormViewMixin, CreateView) template_name = "create_car.html" form_class = ModelChainForm model = Car
视图
每当父字段更改时,都会进行ajax调用。必须设置ajax url以返回json列表:
class AjaxChainedView(BaseDetailView): """ View to handle the ajax request for the field options. """ def get(self, request, *args, **kwargs): field = request.GET.get('field') parent_value = request.GET.get("parent_value") vals_list = [] for x in range(1, 6): vals_list.append(x*int(parent_value)) choices = tuple(zip(vals_list, vals_list)) response = HttpResponse( json.dumps(choices, cls=DjangoJSONEncoder), mimetype='application/javascript' ) add_never_cache_headers(response) return response
或者您可以使用ChainedSelectChoicesView类助手,如下所示:
class AjaxChainedView(ChainedSelectChoicesView): def get_choices(self): vals_list = [] for x in range(1, 6): vals_list.append(x*int(self.parent_value)) return tuple(zip(vals_list, vals_list))
或者像这样:
class AjaxChainedView(ChainedSelectChoicesView): def get_child_set(self): return ChildModel.object.filter(parent_id=self.parent_value)
别忘了更新你的url.py:
url(r'^ajax/custom-chained-view-url/$', AjaxChainedView.as_view(), name='ajax_chained_view'),