Django管理过滤器,在模板中添加两个字段

时间:2022-09-12 08:52:38

I wanted to create a filter to get some value range. there I want to get 2 numbers in to parameter_name for that i added 2 number field. like bellow image.

我想创建一个过滤器来获得一些值范围。在这里,我想在parameter_name中输入两个数字,为此我添加了两个数字字段。像拉风箱的形象。

Django管理过滤器,在模板中添加两个字段

If I use only one field, value of that field can be get. as value = self.value()

如果我只使用一个字段,就可以得到该字段的值。作为价值= self.value()

<input type="number" step="0.1" max="0.9" min="0.1" id="IsWithinRange" name="IsWithinRange">

But i need these 2 values so added another field, then non of their value can get. self.value() always is Non. now I cannot figure out how can I pass this value to my filter. can anyone direct me to the correct path.

但我需要这两个值,所以加上另一个字段,就不会得到它们的值。总是非self.value()。现在我不知道如何将这个值传递给我的过滤器。谁能告诉我正确的路线吗?

1 个解决方案

#1


1  

This may be not the exact answer. but using this you might be able to fulfill the requirement.
※If there are more than one input fields the Django filter will not work properly. Therefore have to use one input field per a filter.
(Use Jquery slider to select range rather than using two input fields)

这可能不是确切的答案。但是用这个你可能会满足要求。※如果有多个输入字段,Django过滤器将无法正常工作。因此,每个过滤器必须使用一个输入字段。(使用Jquery滑动条选择范围,而不是使用两个输入字段)

Python class

Python类

class IsWithinRangeFilter(admin.SimpleListFilter):

title = 'Title'
parameter_name = 'scoreRange'
template = '[path]/input_filter.html'

def lookups(self, request, model_admin):
    return (
        ('Yes', 'Yes'),
    )

def queryset(self, request, queryset):

    value = self.value()
    try:
        if value:

            #do whatever

    except Exception as e:
        logger.info('#####ERROR{}' .format(e))

    return queryset

call the filter class

调用过滤器类

list_filter = (IsWithinRangeFilter,)

input_filter.html

input_filter.html

{% load i18n %}

<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script>
    var lowerRange = 5;
    var upperRange = 8;   

    $( function() {
      $( "#slider-range" ).slider({
        range: true,
        min: 0,
        max: 10,
        values: [ lowerRange, upperRange ],
        slide: function( event, ui ) {
            if(ui.values[ 0 ] === ui.values[ 1 ])
            {
                return false;
            }
            $( "#scoreRange" ).val( ui.values[ 0 ]/10 + " - " + ui.values[ 1 ]/10 );
        }
      });
      $( "#scoreRange" ).val( $( "#slider-range" ).slider( "values", 0 ) / 10 +
        " - " + $( "#slider-range" ).slider( "values", 1 ) / 10 );
    } );
    
</script>

{% block content %}
<h3>{% Any Title %}</h3>

<ul>
    <li>
        <form>
            <p>
                <label >Any Description</label>
                <div id="slider-range"></div>
                <label style="font-weight: bold;">Filter:</label><input type="submit" id="scoreRange" name="scoreRange" style="margin:5px; text-align:center; background-color:#79aec8; cursor:pointer; border:0; color:#f7f5f3; font-weight:bold;">
            </p>            
        </form>
    </li>
</ul>
{% endblock %}

hope this will help!!!

希望这将帮助! ! !

#1


1  

This may be not the exact answer. but using this you might be able to fulfill the requirement.
※If there are more than one input fields the Django filter will not work properly. Therefore have to use one input field per a filter.
(Use Jquery slider to select range rather than using two input fields)

这可能不是确切的答案。但是用这个你可能会满足要求。※如果有多个输入字段,Django过滤器将无法正常工作。因此,每个过滤器必须使用一个输入字段。(使用Jquery滑动条选择范围,而不是使用两个输入字段)

Python class

Python类

class IsWithinRangeFilter(admin.SimpleListFilter):

title = 'Title'
parameter_name = 'scoreRange'
template = '[path]/input_filter.html'

def lookups(self, request, model_admin):
    return (
        ('Yes', 'Yes'),
    )

def queryset(self, request, queryset):

    value = self.value()
    try:
        if value:

            #do whatever

    except Exception as e:
        logger.info('#####ERROR{}' .format(e))

    return queryset

call the filter class

调用过滤器类

list_filter = (IsWithinRangeFilter,)

input_filter.html

input_filter.html

{% load i18n %}

<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script>
    var lowerRange = 5;
    var upperRange = 8;   

    $( function() {
      $( "#slider-range" ).slider({
        range: true,
        min: 0,
        max: 10,
        values: [ lowerRange, upperRange ],
        slide: function( event, ui ) {
            if(ui.values[ 0 ] === ui.values[ 1 ])
            {
                return false;
            }
            $( "#scoreRange" ).val( ui.values[ 0 ]/10 + " - " + ui.values[ 1 ]/10 );
        }
      });
      $( "#scoreRange" ).val( $( "#slider-range" ).slider( "values", 0 ) / 10 +
        " - " + $( "#slider-range" ).slider( "values", 1 ) / 10 );
    } );
    
</script>

{% block content %}
<h3>{% Any Title %}</h3>

<ul>
    <li>
        <form>
            <p>
                <label >Any Description</label>
                <div id="slider-range"></div>
                <label style="font-weight: bold;">Filter:</label><input type="submit" id="scoreRange" name="scoreRange" style="margin:5px; text-align:center; background-color:#79aec8; cursor:pointer; border:0; color:#f7f5f3; font-weight:bold;">
            </p>            
        </form>
    </li>
</ul>
{% endblock %}

hope this will help!!!

希望这将帮助! ! !