0

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

  • What shell & OS are you using? – Nealium Oct 21 '22 at 00:39
  • I am using VS Code's own terminal to run the shell. OS is Windows 10. It executes other codes such as print(fruits[0[). I can see the output in the console as apple. It just doesn't execute the for loop for some reason – ControltheAI Oct 21 '22 at 09:39
  • I've never used VS Code (I'm a Cmder guy), but it's probably just some setting.. Maybe try changing the terminal VSC uses to cmd or powershell or git bash – Nealium Oct 21 '22 at 14:59
  • 1
    as far as I know, running on VS Code terminal is same running something in MS command prompt or powershell – ControltheAI Oct 22 '22 at 20:18

1 Answers1

0

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.

alexakarpov
  • 1,848
  • 2
  • 21
  • 39