django-xadmin数字输入框不支持小数点小数问题

环境:https://github.com/y2kconnect/xadmin-for-python3.git python3.5.2 django1.9.12

原因:数字输入框用的是html5 input number元素,因未定义step属性,所以只能输入整数,不能输入小数

 

解决办法:

第一步:新增文件xadmin\templatetags\xadmin_filters.py内容如下:
from django import template
import math
register = template.Library()


@register.filter(is_safe=False)
def number_step(decimal_places):
    """根据decimal_places小数位数返回input的step值"""
    try:
        return 1/math.pow(10, decimal_places)
    except Exception as e:
        return ''

 


第二步:xadmin\templates\xadmin\filters\number.html 增加input number输入框的step属性,以下为更改好后的number.html源码
{% load i18n %}
{% load xadmin_filters %}
<li class="dropdown-submenu filter-number">
  <a><i class="fa fa-filter {% if spec.is_used %}text-success{%else%}text-muted{% endif %}"></i> {{ title }}</a>
  <div class="popover right">
    <div class="arrow"></div>
    <h3 class="popover-title">
      {% trans "Search" %} {{title}}
    </h3>
    <div class="popover-content">
      <form method="get" action="">
        {{ form_params|safe }}
          <div class="input-group">
            <span class="input-group-addon">=</span>
            <span class="input-group-btn">
              <a class="btn btn-default toggle{%if ne_val%} active{%endif%}" data-toggle="button" data-off-name="{{equal_name}}" data-on-name="{{ne_name}}">!</a>
            </span>
            <input name="{%if ne_val %}{{ne_name}}{% else %}{{equal_name}}{%endif%}" class="form-control" type="number" value="{%if ne_val %}{{ne_val}}{% else %}{{equal_val}}{%endif%}" placeholder="{% trans "Enter Number" %}" {%if spec.field and spec.field.decimal_places %}step="{{spec.field.decimal_places|number_step}}"{%endif%}/>
            <span class="input-group-btn"><a class="btn btn-default remove">x</a></span>
          </div>
          <div class="input-group">
            <span class="input-group-addon">></span>
            <span class="input-group-btn">
              <a class="btn btn-default toggle{%if gte_val%} active{%endif%}" data-toggle="button" data-off-name="{{gt_name}}" data-on-name="{{gte_name}}">=</a>
            </span>
            <input name="{%if gte_val %}{{gte_name}}{% else %}{{gt_name}}{%endif%}" class="form-control" type="number" value="{%if gte_val %}{{gte_val}}{% else %}{{gt_val}}{%endif%}" placeholder="{% trans "Enter Number" %}" {%if spec.field and spec.field.decimal_places %}step="{{spec.field.decimal_places|number_step}}"{%endif%}/>
            <span class="input-group-btn"><a class="btn btn-default remove">x</a></span>
          </div>
          <div class="input-group">
            <span class="input-group-addon"><</span>
            <span class="input-group-btn">
              <a class="btn btn-default toggle{%if lte_val%} active{%endif%}" data-toggle="button" data-off-name="{{lt_name}}" data-on-name="{{lte_name}}">=</a>
            </span>
            <input name="{%if lte_val %}{{lte_name}}{% else %}{{lt_name}}{%endif%}" class="form-control" type="number" value="{%if lte_val %}{{lte_val}}{% else %}{{lt_val}}{%endif%}" placeholder="{% trans "Enter Number" %}" {%if spec.field and spec.field.decimal_places %}step="{{spec.field.decimal_places|number_step}}"{%endif%}/>
            <span class="input-group-btn"><a class="btn btn-default remove">x</a></span>
          </div>

          <button type="submit" class="btn btn-success">{% trans "Apply" %}</button>
        {% if spec.used_params %}
          <a class="btn btn-default" href="{{remove_url}}">{% trans "Clean" %}</a>
        {% endif %}
      </form>
    </div>
  </div>
</li>

 

posted @ 2017-05-18 21:21  大棚  阅读(724)  评论(0编辑  收藏  举报