Flask: AttributeError: 'NoneType' object has no attribute 'split'

Question:

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:UsersPJDesktopKalendermain.py", line 48, in start

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

File "C:UsersPJDesktopKalendermain.py", line 12, in numberinstring

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

Can somebody help me with this?

Asked By: phegaM

||

Answers:

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
Answered By: Maxwell D. Dorliea
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.