Showing posts with label templates. Show all posts
Showing posts with label templates. Show all posts

Saturday, March 8, 2014

Templates

What's a programmer's favorite part of programming? The finished product? The process of learning a new skill to implement some functionality? Building new worlds to explore? Sure these are all reasons why people might enjoy programming, but I'm going to explain the strange reason why I enjoy programming.

I was working on our website, and was trying to change our view from delivering a simple HTTPResponse() text string to delivering a rendered template file. In doing this, I tried to follow the Django tutorial, but I kept on receiving an error that the template file could not be located. Googling for solutions to the problem just returned results about making sure that template files for specific models are stored with the models. This isn't what I was trying to accomplish. I just wanted a general view for the website. The error explained that it searched for templates in: 
.../projectHAM/templates/site/homepage.html
 but couldn't find the resource specified.

"Umm, excuse me?" I said, "I clearly have that template located in the directory. Just look at my directory structure!"

projectHam/templates/site/homepage.html

Clearly there was something wrong with Django, because surely I couldn't have been doing anything wrong. How can I correct an error that the template isn't in the right space when it clearly is located in the right spot. Frustrated and hungry because it was 6:45 pm and I hadn't eaten yet, I left the obstinate computer in a huff.

After coming back from dinner, I sat down to try and make a deal with the computer. Maybe it would be willing to work with me after it had time to cool down a little. It wasn't long until I was able to fix the problem. Can you guess what my error was?

I mentioned earlier that the error said that the template should be located at 
.../projectHAM/templates/site/homepage.html
which is the way I read it when I was trying to solve the problem. What the error actually said was
.../workspace/projectHAM/templates/site/homepage.html
while I had my templates located in
 .../projectHAM/projectHAM/templates/site/homepage.html
I had spent probably 45 minutes trying to figure out what was wrong with Django when I was the one that placed the templates folder in the wrong directory. After this glorious, "Aha!" moment I moved the template directory to the proper place, reloaded the page, and saw the error screen no more.

This enormous struggle that I had that ended in that wonderful moment when you I realized what I had done wrong and exactly what I needed to do to fix it is why programming is so much fun for me. Those small moments when your world is consumed by a problem and then you finally fix it and everything feels right.

With that said, there are two lessons to take away from this:

  1. If you spend more than 30 minutes trying to fix an error and aren't sure what to do just drop it, walk away and come back to the problem later. A fresh perspective is one of the most useful tools for solving a problem.
  2. Read the whole directory structure, of where template files are supposed to be located. It will save you approximately 45 minutes of struggling with what seems to be an impossible problem.
Now that our website uses templates for returning views, we can do some pretty cool features, like identifying our users by name when they log in. Here is our new "congrats, you're logged in" screen.




Monday, March 3, 2014

Amber's Success

After a long period of struggling, Amber has finally experienced success with her user story! Amber was able to access the website and log in using her name and a password. Amber is slightly confused by the website though, all that shows up is some small 12pt font in the upper left corner that lets her know that she has accessed the website. She then has to add "/login" to the address bar to bring up the log in screen so that she can enter her credentials. After logging in, a familiar mostly blank screen appears with some different text this time that says "You are now logged in!" Even more confusing, in order to log out, she has to navigate to a "/logout" page. Not the most useful website in the world, but she feels secure knowing that she has an account with this website.

Finding out how to get a standard user to log in with Django's framework proved to be a fairly complex process. After wading through seemingly endless looping links in the Django documentation, I finally stumbled on this page that seemed to be just what I needed. I found that looking at the "login_required" section was particularly helpful. Adding this redirected me to a predefined URL which should host the log in page. Unfortunately, there was an error with displaying this log in page. I was probably missing some obvious piece of code, but it seemed to me that the django directory was missing some template files. By searching in the directories for this log in template, I was able to come across another log in template for the admin half of the site. I pasted this template into the directory that the log in page was looking for the template and I had an instant (though incorrectly labelled) log in page. Using my knowledge from the Django tutorial, I put together the simple text pages that greet the user to the website and notify them that they have logged in.

I hope to come up with a more elegant solution to this log in problem in the future, but for the moment, it feels so good to have completed this first step.