[ Duplicate Modules ]

In a Django app, I have an extra module. So my app is called header and inside it I have a model, view and a header file. Inside of the header file I have a class called resources which does what I want, but for some reason on a page refresh, it duplicates the information.

view:

def home(request):
header = Resources("home") #generate the resources for this page

return render_to_response('pages/index.html', {'header': header, 'content': test})

resources:

class Resources(object):

javascript_resources = []
javascript_files = []
style_sheets = []

def __init__(self, page_id = "home"):
    self.page_id = page_id.lower()
    self.get_status()
    self.get_meta()
    self.get_javascript()
    self.get_style_sheets()

def get_status(self):

def get_meta(self):

def get_javascript(self):

def get_style_sheets(self):

So what happens is, the header.javascript_resources list should only be 3 members long, but each duplicate it adds to it. So for some reason this object is staying in memory, even after a page refresh. What is causing this?

Answer 1


javascript_resources is an attribute of the Resource class, so if you are adding stuff to it in subsequent requests, they will "add up".

If you want that each request has an empty javascript_resources, make it an attribute of the instance (i.e. initialize it in __init__) or empty it before instanciating Resource.

Answer 2


The problem seems to be that you created javascript_resources as class variable. Try to move its initialisation in the __init__ method.