Django模板中未重新分配变量

时间:2020-09-15 09:53:07

标签: python html django django-templates

我正在尝试在for循环中重新分配变量。根据该值,在将其重置为其他默认值之前,我将继续执行其他操作。

但是,该值未在循环中正确替换。

这些是我的模板标签:

"scripts": { "pre-autoload-dump": "cp ./vendor/mailchimp/marketing/lib/Configuration.php ./vendor/mailchimp/marketing/lib/ApiClient.php" },

这是我完整模板的一部分

structure(list(Stamp_summertime = structure(c(1546684744, 1546685858, 
                                              1546687004, 1547030061, 1547030835, 1547031816), tzone = "UTC", class = c("POSIXct", 
                                                                                                                        "POSIXt")), X26.013 = c(0.138461, 0.138461, 0.138461, 0.144421, 
                                                                                                                                                0.144421, 0.144421), X27.024 = c(0.0752111, 0.0752111, 0.0752111, 
                                                                                                                                                                                 0.0426819, 0.0426819, 0.0426819), X33.031 = c(3.75788, 3.75788, 
                                                                                                                                                                                                                               3.75788, 3.12581, 3.12581, 3.12581), jar_camp = c("1_pf1.1", 
                                                                                                                                                                                                                                                                                 "2_pf1.1", "3_pf1.1", "1_pf2.1", "2_pf2.1", "3_pf2.1"), jar = structure(c(1L, 
                                                                                                                                                                                                                                                                                                                                                           12L, 23L, 1L, 12L, 23L), .Label = c("1", "10_blank", "11", "12", 
                                                                                                                                                                                                                                                                                                                                                                                               "13", "14", "15", "16_blank", "17", "18", "19", "2", "20_blank", 
                                                                                                                                                                                                                                                                                                                                                                                               "21", "22", "23", "24", "25", "26", "27", "28", "29", "3", "30_blank", 
                                                                                                                                                                                                                                                                                                                                                                                               "31", "32", "33", "34", "35", "36", "37", "38_blank", "39", "4", 
                                                                                                                                                                                                                                                                                                                                                                                               "40", "41", "42", "43", "44_blank", "45", "46", "47", "48", "49", 
                                                                                                                                                                                                                                                                                                                                                                                               "5_blank", "blank_50", "51", "52", "53", "54", "55", "56", "57", 
                                                                                                                                                                                                                                                                                                                                                                                               "6", "7", "8", "9", "X_blank"), class = "factor"), campaign = c("pf1.1", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                               "pf1.1", "pf1.1", "pf2.1", "pf2.1", "pf2.1"), i.X26.013 = c(0.144658, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           0.21502, 0.458296, 0.191571, 0.0789067, 0.711814), i.X27.024 = c(0.0595547, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            0.0651149, 0.146772, 0.0997815, 0.0539976, 0.185398), i.X33.031 = c(5.4066, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                3.30406, 18.0479, 6.13854, 1.3028, 22.2226)), sorted = "Stamp_summertime", class = c("data.table", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "data.frame"), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x00000237a3d91ef0>)

我的输出是这样的

class CarSpeed:
    def __init__(self,name,dist,time):
        self.dist=dist
        self.time=time
        self.name=name
    
    def ans(self):
        speed= self.dist/self.time
        return speed
        
    
    
   
speed_list = {}
Ford=CarSpeed("Ford",120,1.75)
speed_list.update({Ford.name: Ford.ans()})

Ferrari=CarSpeed("Ferrari",100,1.20)
speed_list.update({Ferrari.name: Ferrari.ans()})

BMW=CarSpeed("BMW",205,2.35)
speed_list.update({BMW.name: BMW.ans()})

Porsche=CarSpeed("Porsche",155,1.85)
speed_list.update({Porsche.name: Porsche.ans()})

Audi=CarSpeed("Audi",190,2.10)
speed_list.update({Audi.name: Audi.ans()})

Jaguar=CarSpeed("Jaguar",255,2.45)
speed_list.update({Jaguar.name: Jaguar.ans()})

print(dict([max(speed_list)]))

为什么我的@register.filter def addition(val1, val2): return val1 + val2 @register.simple_tag def assignVar(arg1): return arg1 值没有增加? 每次循环时,{% assignVar 50 as testVar %} <table> <tbody> {% for data in LIST %} <tr> A: {{testVar}} {% assignVar testVar|addition:20 as testVar %} B: {{testVar}} {% if testVar > 150 %} {{data}} {% assignVar 0 as testVar %} {% endif %} </tr> {% endfor %} </tbody> </table> 变量都会一直返回到初始值,而不是被新变量替换。

这是我所期望的:

A: 50
B: 70
A: 50
B: 70
A: 50
B: 70
...

0 个答案:

没有答案