Django Shell Command Does Not Execute For Loop

413 Views Asked by At

I have a Django project going on, containing some functions and creating/updating a database. From time to time, I need to update the staticfiles in the project therefore I created a .py file in the same folder as the project named updatefiles.py

I am running the script as python manage.py shell <updatefiles.py. Inside this file there is a for loop but for some reason it does not execute the for loop. I have tried even with a simple for loop such as below but it doesn't execute that one as well. Anyone has any ideas?

fruits = ["apple", "banana", "cherry"]

for x in fruits:
    print(x)

The output is:

(InteractiveConsole)
>>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> ... ... ... ... ... ... ... ... ... ... ...
now exiting InteractiveConsole...

Anyone has any ideas?

Edit: For anyone having the same issue, I solved it by changing the structure. I defined a function within settings.py and put the for loop inside this function. Then inside updatefiles.py I simply call this function. By running python manage.py shell <updatefiles.py it works now

1

There are 1 best solutions below

0
alexakarpov On

I think what you really want to do is build a custom Django command:

https://docs.djangoproject.com/en/4.1/howto/custom-management-commands/

Putting a function inside the settings.py is not a good idea.