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

7👍

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.

0👍

Just use blueprints and give your view the endpoint date.today

0👍

Are you using the word "date" as a variable anywhere else in your function or script? The problem is caused because you have used the date variable in your code. Sometimes it is not possible to change or rename all the variables if we used them more than 500 times. It will change the functionality of a program.
So the solution is, instead of importing the module as, from datetime import date, you can use an alias name like this: from datetime import date as date_function

then from this:

today = date.today()

change to this:

today = date_function.today()

Hope this is helpful for you!

👤Raghul

Leave a comment