线性比例Django表单小部件 [英] Linear Scale Django form widget

查看:39
本文介绍了线性比例Django表单小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些指导,以创建一个在django文档中完全没有记载但在表单中显而易见的小部件.我在说的是用于表格的线性比例小部件.使用户能够在1-100之间的分数范围内进行选择

I am looking for some guidance in order to create a widget that is not documented at all in the django documentation but is an evident part of forms. And I am talking about Linear Scales widgets for forms. Giving the ability to a user to choose on a scale between 1-100 a score

如果您对如何操作有任何想法,请给我任何指示;)

If you have any idea on how to do it, any direction I would be please to get it ;)

非常感谢您

推荐答案

我猜想您想要一个 type ="range" 输入,您可以在此

I'm guessing that you want a type="range" input as you can find in this MDN page.

Django不提供这种字段开箱即用,则您必须自己完成操作或找到一个实现它的库.

Django doesn't provide this kind of field out of the box and you have to do it yourself or find a library that implements it.

要实现字段,您应该查看字段API 和输入 API .

To implement a field you should look at the Field API and the Input API.

# example input
from django.form.widgets import Input

class RangeInput(Input):
    input_type = 'range'
    template_name = 'path/to/input/template.html'

还应该实现类型强制(转换为float的整数)和范围检查,以确保可以验证表单并将其适当地存储在db中.

You should also implement type coercion (to int of float) and range checking to be sure that you can validate a form and store it appropriately in the db.

完成后,您可以使用此输入创建表单,或为模型创建新的 RangeField 并将其链接到 RangeInput 元素.

Once you have done that you can create your form with this input or create a new RangeField for models and link it to the RangeInput element.

这篇关于线性比例Django表单小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆