Home > Enterprise >  Flask: AttributeError: 'NoneType' object has no attribute 'split'
Flask: AttributeError: 'NoneType' object has no attribute 'split'

Time:11-13

Im new to Python/Flask and wanted to set Cookies for my Website. When i open the Website i get this error.

The functions that are causing Problems:


def numberinstring(nr: int, cookie: str):
          
        visited = cookie.split(":")

        for door in visited:
                if nr == int(door):
                        return True
        return False

def handlecookie(resp: Response, nr: int):

        cookie = request.cookies.get("Besucht")

        if cookie is None:
                resp.set_cookie("Besucht", str(nr))
                return resp

        if numberinstring(nr, cookie):
                return resp

        resp.set_cookie("Besucht",cookie   ":"   str(nr))
        return resp

I am confused to why it doesent work as I checked if cookie is None in line 15.

The Traceback:


File "C:\Users\PJ\Desktop\Kalender\main.py", line 48, in start

if numberinstring(i, cookie):
   ^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\PJ\Desktop\Kalender\main.py", line 12, in numberinstring

visited = cookie.split(":")
          ^^^^^^^^^^^^

Can somebody help me with this?

CodePudding user response:

request.cookies.get("Besucht") will return None if there's no KEY with name "Besucht" so check if cookie has value before calling the split method.

def numberinstring(nr: int, cookie: str):
      visited = ""
      if cookie:
          visited = cookie.split(":")

      for door in visited:
          if nr == int(door):
              return True
      return False
  • Related