I have subsequently invoked
sudo chown -R www-data templates
This is my practice whenever I make changes from the command line -- it saves a lot of headaches. In fact I make sure that the entire webwork/courses directory and its subdirectories are owned by the webserver. It's also one reason why I often do things from the web even though I could do them slightly quicker from the command line. It's not quicker if I forget to get the permissions right when I am done.
One other trick which can help. You can first do
sudo su www-data
so that you are acting as the webserver when you make command line changes. (su stands for switch user).
Thanks for the report.