I am still in the development phase of a Django App. Before even writing my views.py, I test them out to see if my models are correctly defined. This I do it in the terminal by invoking
python manage.py shell
But oh so often I make some syntax error prompting me to abort the shell ctrl-D & retype everything. This process is taking forever. It would be better if I could write all this in some file just for my trials & if all’s well copy it to views.py.
What’s the process for this? Is it as simple as creating a trial.py in my app directory. Won’t I have to import the Django env? What’s the best way to do this?
By all means create a trial.py for simple experimentation, then after doing
you can do
and then invoke the code in trial, directly from the prompt, e.g.
If you need to change things you can just save your changed trial.py and do
Of course, you may need to recreate any existing objects in the interactive session in order to make use of your changes.
This should be seen as complementary to writing unit tests (as per Jani’s answer), but I do find this approach useful for trying things out using iterative refinement.