you are calling the function twice, appending every element, and then 
appending the whole list!


Le samedi 8 novembre 2014 18:07:04 UTC+1, monoBOT monoBOT a écrit :
>
> Lists are inmutable!!!! You are modifiying the same list everytime you 
> invoque that view
> El 07/11/2014 10:08, "termopro" <term...@gmail.com <javascript:>> 
> escribió:
>
>> I have a view in Django which calls external library/class. The problem 
>> is that for some reason Django keeps caching results coming from previous 
>> calls of that class.
>>
>> Please consider the following simple example:
>>
>> Django view:
>>
>> from some_path import Demodef test_view(request):
>>     demo = Demo()
>>     result = demo.do_something()
>>     return render(request, 'test.html',
>>                             { 'result':result }
>>                 )
>>
>> Demo class:
>>
>> class Demo():
>>     result = []
>>
>>     def do_something(self):
>>         self.result.append(1)
>>         self.result.append(2)
>>         self.result.append(3)
>>         return self.result
>>
>> You expect result to be [1, 2, 3], right ? WRONG!
>>
>> The first time a page is loaded you'll get the correct result. But on all 
>> following requests it will keep incrementing: [1, 2, 3, 1, 2, 3]... [1, 2, 
>> 3, 1, 2, 3, 1, 2, 3] ...
>>
>> So my question is obvious - what is going on here ? How do i receive [1, 
>> 2, 3] every time i call a class inside Django view ?
>>
>> p.s. - Django 1.7 / MacOSx
>>  
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users...@googlegroups.com <javascript:>.
>> To post to this group, send email to django...@googlegroups.com 
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/72ee993f-cc34-4aee-9455-694b09148d8d%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/72ee993f-cc34-4aee-9455-694b09148d8d%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/762d3363-5db5-4e10-8ae1-1b603b11e969%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to