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

4
  • What shell & OS are you using? Commented Oct 21, 2022 at 0: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 Commented Oct 21, 2022 at 9: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 Commented Oct 21, 2022 at 14:59
  • 1
    as far as I know, running on VS Code terminal is same running something in MS command prompt or powershell Commented Oct 22, 2022 at 20:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.