Python: "is not None" not working as expected

时间:2015-05-04 19:22:17

标签: python django python-2.7 django-views

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

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.

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.

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.

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

<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 个答案:

答案 0 :(得分:7)

"" 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)

答案 1 :(得分:1)

您可能想尝试简单的RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L] 。这是一个比if slider_value1 != ''更好的选择,因为后者会阻止if not slider_value1您可能不想做的事情。