Forums

Use tasks to run django shell?

Is there any way to run the Django shell with all of my models that I can import on a task?

Looks like custom management command would be a much better way to do it.

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

So I made the management command and I have it working, but how would I run this with a task file? Would I need to create a .sh file? And what would this look like?

Put something like

/path/to/python/you/want/to/use/python /home/username/path/to/your/project/manage.py yourcommand

into your scheduled task command box. See https://help.pythonanywhere.com/pages/ScheduledTasks

Do you run your django in virtual environment? If so, see https://help.pythonanywhere.com/pages/VirtualEnvInScheduledTasks

Thankyou @fjl it worked for me something like:

python "/path_to_manage.py/manage.py" shell < "path_to_script/script.py"

Interesting workaround! (You're not using management command capabilities though :))

I am having exactly same issue and only the command the shell command (python "/path_to_manage.py/manage.py" shell < "path_to_script/script.py") is the one that I have been able to make it work. How can we set it up to use the management command capabilities Base on the documentation I thought it will be something like:

python3.8 /home/username/.virtualenvs/enviromentname/bin/python /home/username/....../task_dashboard.py

but this throughs the error File "/home/businessfuel/.virtualenvs/sandbox/bin/python", line 1 SyntaxError: Non-UTF-8 code starting with '\x88' in file /home/username/.virtualenvs/sandbox/bin/python on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

so what is the right command?

You appear to be trying to use python3.8 to run the python binary from your virtualenv. That is why you're getting that error. Remove the python3.8 from the start of that command.