I am making a Flask website, but the replace() method isn't working

I am making a Flask website, but I am having problems while templating. I believe that the problem is with the .replace() function, because every other page in the webserver is working, but the one that uses .replace() is the only one that is giving a problem.

def portfolio():
  today = datetime.date.today()
  page = ""
  f = open("templates/Portfolio.html", "r")
  page = f.read()
  page.replace("{today}", today)
  f.close()
  return page
1 Like

I have already imported datetime, so the error can’t be that.

can anyone help pls??

It says TypeError: replace() argument 2 must be str, not datetime.date

to fix this, just add today = str(today) after today = datetime.date.today()

It still won’t work though because even though you are replacing {today} with the current date, you actually aren’t making that change to page.

You need to change page.replace("{today}", today) to
page = page.replace("{today}", today)

def portfolio():
  today = datetime.date.today()
  today = str(today) #here
  page = ""
  f = open("templates/Portfolio.html", "r")
  page = f.read()
  page = page.replace("{today}", today) #here 
  f.close()
  return page
2 Likes

Thanks!
That helped a lot…
I completely forgot I had to cast today + I didn’t actually save the variable page after I edited it…
ty

1 Like

If that solved your issue, please press the solution button next to this post.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.