How to run scripts outside views.py?

I have a very trivial question so please forgive me.
Until this time I always worked in the views.py files. Now I made a script called anything.py and I like to run it.

In this file there is nothing special just a send_mail function. If I run the server it does nothing. If I try to use the script in the views.py it works fine.

What should I do?

When do you want it to run?

  • Does it run on a schedule?

  • Does it run based on an event in the system?

  • Does it run based on an event outside the system?

  • Does it run completely outside the context of the server?

Hi Ken! Thank you for your answer.
My goal is to send monthly scheduled emails for specific users with crontab or with schedule (GitHub - dbader/schedule: Python job scheduling for humans.) but I’m stuck in the first step. I never did something like this so just like to be more familiar how these tasks go.

I generally recommend something like this be created as a Django custom management command. You would then run this using the “manage.py” script, the same way you would run any other management command.