Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : local variable 'date' referenced before assignment but I import it

I am building a script that use the datetime module :

def workspace_detail(request, token):
yesterday = date.today() -  timedelta(days=1)
tomorrow = date.today() - timedelta(days=1)
quicklink = f"{token}start_date={yesterday}&end_date={tomorrow}"
w_yesterday = quicklink

But I have this error

local variable 'date' referenced before assignment

I imported it every module from the datetime package

from datetime import datetime, date, timedelta

If I use datetime.today() it works, but I want to use the date.today() for my url.

Thanks


1 Answers

Are you using the word "date" as a variable anywhere else in your function or script?.

This happened to me and the fix was to rename any variable named "date" and only use that keyword for the date object.

like image 80
Debo Akeredolu Avatar answered Sep 07 '25 20:09

Debo Akeredolu