使用ajax请求的django框架的链式选择框小部件。

django-clever-selects的Python项目详细描述


这个库的灵感来自于s-blockhttps://github.com/s-block/django-chained-selectbox)。

它使用ajax请求将选择框链接在一起,为django框架提供链式选择框小部件。 值根据父值而变化。

前面提到的库仅用于django管理。新库具有前端功能, 改进现有的实例数据初始化和新的^ {TT1} $。它还使用自定义的testclient 如果用户已登录,则可以将request.user变量传递到ajax视图中。如果您需要根据 例如,用户权限。

在Django 1.4.5测试。

要求

  • Django
  • jquery

安装

  1. 使用pip安装python库:pip install django clever selects
  2. clever_selects添加到django设置文件中的INSTALLED_APPS
  3. {% 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'),

作者

图书馆由Pragmatic Mateserik telepovsky提供。见our other libraries

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JPA。Eclipselink没有为mySQL提供密码,但它应该提供   我的Servlet和@FormDataParam存在java问题   java将什么作为上下文参数传递到文件I/O方法中?   如果两个值相同,java无法找到其中一个单选按钮   java在变量和方法名中使用下划线   JavaSpringMVC单线程安全?   klazz类的java Arraylist(反射Api)   java如何在数字字符串中查找最频繁的数字?   JavaAPI设计:使数据更易于阅读与强制更多API调用   JavaHadoopMapReduceforGoogleWebGraph   java无法启动gauge API:Runner意外退出   java如何在bluemix上使用ibm工作负载调度器?   拉取一年中某一周特定日期的所有日期   java为什么是我的角节点。js应用程序将图像上传到S3� 邮递员正确上传时的符号?   在不使用任何第三方jar的情况下将文件从本地传输到linux系统(java代码)   java将现有文件夹复制到Eclipse工作区中新创建的项目中   Java中的regex RegExp帮助   当使用“系统”外观时,Java组合框setSelectedItem会出现故障   JavaASM:在类的方法中获取局部变量名和值