Python:“不是无”没有按预期工作

时间:2021-03-05 15:22:40

In a multi-page survey application I am creating I have a jQuery UI slider bar which is used to provide a rating for an image. This returns a numerical value to my Python/Django view which is stored in a list slider_DV_values

在我创建的多页调查应用程序中,我有一个jQuery UI滑块,用于为图像提供评级。这会将一个数值返回到我的Python / Django视图,该视图存储在列表slider_DV_values中

On a later Data Verification survey page the participant is given the opportunity via another jQuery slider bar to update the rating they assigned the image.

在稍后的数据验证调查页面上,参与者可以通过另一个jQuery滑动条获得机会,以更新他们为图像分配的评级。

My issue is that the jQuery UI slider bar only returnes a numerical value if the participant changes it. Therefore the original rating is getting overwritten, with nothing, if the participant does not update it.

我的问题是,如果参与者更改它,jQuery UI滑块栏只会返回一个数值。因此,如果参与者没有更新,则原始评级将被覆盖,没有任何内容。

However if they do update their rating the new value is getting stored.

但是,如果他们确实更新了他们的评级,则会存储新值。

If I try

如果我试试

    elif step == 13:
        slider_value1 = self.request.POST.get('slider_value1')

        print "This is slider_value1", slider_value1

        if slider_value1 is not None:
            slider_DV_values.pop(0)                
            slider_DV_values.insert(0, slider_value1)

The original values stored in slider_DV_values are still getting overwritten, with nothing.

存储在slider_DV_values中的原始值仍然被覆盖,没有任何内容。

I thought the is not None would have prevented an empty value from being used to overwrite the original value? IS this not correct?

我认为不是没有阻止空值被用来覆盖原始值?这不正确吗?

Can anyone tell me how to prevent the original values from getting overwritten unless the new value is an updated numerical value?

任何人都可以告诉我如何防止原始值被覆盖,除非新值是更新的数值?

Thanks, Deepend

EDIT

To see how I am getting my values this is the jQuery slider bar in a page of my SurveyWizardView the value of which is returned via the hidden form element

要查看我如何获取我的值,这是我的SurveyWizardView页面中的jQuery滑块,其值通过隐藏的表单元素返回

<div class="DV_image_row">          
        <div class="DV_image_left">             
            <img src="{% static "survey/images/pathone/" %}{{first_image}}{{fourth_image}}{{seventh_image}}" height="300" width="250" style="border:1px solid black;" align="middle"/>                          
                      <div class="DV_slider_one" id="one"></div>                        

                        <script >                                                                               
                            $('#submit').click(function() {
                                var username = $('#hidden').val();
                                if (username == "") username = 0;  
                                $.post('comment.php', {
                                    hidden: username
                                }, function(return_data) {
                                    alert(return_data);
                                });
                            });

                            $(".DV_slider_one").slider({            
                                animate: true,
                                range: "min",
                                value: {{first_slider}}{{forth_slider}}{{seventh_slider}},
                                min: -100,
                                max: +100,
                                step: 1,

                                slide: function(event, ui) { 
                                  $("#slider-result_left").html((ui.value > 0 ? '+' : '') + ui.value);

                                  if($(this).attr("id") ==  "one")
                                      $("#hidden2").val((ui.value > 0 ? '+' : '') + ui.value);
                                }
                            });

                        </script>   


                    <div id="slider-result_left">{{first_slider}}{{forth_slider}}{{seventh_slider}}</div>
                    <input type="hidden" name="slider_value1" id="hidden2"/>        


      </div> 

2 个解决方案

#1


You might want to try the simple if slider_value1 != '' . This is a better option than if not slider_value1 because the latter will block out 0 also which you might not want to do.

您可能想尝试简单的如果slider_value1!=''。这是一个比如果不是slider_value1更好的选择,因为后者将阻止你也可能不想做的0。

#2


"" is None is False

“”是无是假的

ONLY None is None is True

只有没有是没有是真的

perhaps you just want if not slider_value1 : which is true for ANY falsey value (empty string,empty list, empty tuple, false, 0 , None , etc)

也许你只想要,如果不是slider_value1:对于任何falsey值都是如此(空字符串,空列表,空元组,false,0,None等)

#1


You might want to try the simple if slider_value1 != '' . This is a better option than if not slider_value1 because the latter will block out 0 also which you might not want to do.

您可能想尝试简单的如果slider_value1!=''。这是一个比如果不是slider_value1更好的选择,因为后者将阻止你也可能不想做的0。

#2


"" is None is False

“”是无是假的

ONLY None is None is True

只有没有是没有是真的

perhaps you just want if not slider_value1 : which is true for ANY falsey value (empty string,empty list, empty tuple, false, 0 , None , etc)

也许你只想要,如果不是slider_value1:对于任何falsey值都是如此(空字符串,空列表,空元组,false,0,None等)