Search code examples
pythonpython-3.xbasehttpserver

Object is not iterable when replacing word called by function


How do I get otherImages to return the string in it so that I can replace a word within it when called from 'narrow' method?

    def otherImages(self):
        self.wfile.write(bytes("<div id='menu_button'><a href='/narrow'><img src='../images/menu_button.png'></a></div>", "utf8"))
                                                               #^word I want to replace
    def contentList(self, skip_name=''):  # All content methods in list 
        methods = [self.title, self.containerDIV, self.heading, self.stopSection, self.offlineSection, self.onlineSection, self.endDIV, self.otherImages]
        for m in methods:
            if m.__name__ != skip_name:
                m()

    def narrow(self):
        try:
            self.reply()
            self.contentList('onlineSection')   # removed onlineSection
            for words in self.otherImages():
                words.replace("narrow", "index")

Solution

  • Here are the changes I made which solves my problem. It allows me to edit the string when called from the 'narrow' method.

    def otherImages(self):
        return["<div id='menu_button'><a href='/narrow'><img src='../images/menu_button.png'></a></div>"]
    
    
    def narrow(self):
        try:
            self.reply()
            self.contentList('onlineSection')   # removed onlineSectionv
            for words in self.otherImages():
                words = words.replace("/narrow", "/")
                self.wfile.write(bytes(words, "utf8"))
            return