为元组赋值

时间:2014-09-29 15:20:49

标签: python django python-2.7

我正在尝试将变量display_image的值分配给元组five_image,并通过get_context_data方法将其返回到html页面。

我知道下面的context.update工作就像我通过five_image = "P5D5.jpg";对元组进行硬编码时在页面上返回正确的值

以下打印功能也正常工作,并将five_image的值打印到终端。

那么为什么两个部分一起不起作用?

 class SurveyWizardOne(SessionWizardView):                      
        def get_context_data(self, form, **kwargs):
            context = super(SurveyWizardOne, self).get_context_data(form, **kwargs)  

            if self.steps.current in ['5','6','7','8','9','10','11','12','13','14','15']:

                display_image = random.choice(path_one_images)   

                if self.steps.current == '5':

                    five_image = ();
                    five_image = display_image 
                    print 'this is five_image', five_image

                context.update({'display_image': display_image,
                                'five_image': five_image,
                                 })

                path_one_images.remove(display_image)

            return context 

2 个答案:

答案 0 :(得分:1)

Python是动态类型的。没有"元组变量"这样的东西只有名字。如果要将元组分配给名称,则需要在赋值的RHS上使用元组:

five_image = (display_image,)

答案 1 :(得分:0)

您无法定义元组,因为我们可以定义列表

five_image = []

如果是单值,则必须使用逗号将元组值放在圆形paranthesis中。

five_image  = (display_image,)

希望它适合你。

相关问题