Friday, May 2, 2008

Django and success

Too bad I can't bring in Borat to give a nice "Success!"

After several days of beating my head against the wall, bugging Derek Willis and Matt Waite to the point they probably would like to shoot me, I finally figured out my Django woes.

For days, I was having any trouble getting beyond Chapter 7 in The Django Book. There's the spot where the author shows you the template. More specifically, the place where you show the search results in the loop.

For the life of me, I couldn't figure out how to show additional data. Waite beat it into my head that it didn't have anything to do with the data request in the view or model --- i.e., it is the equal of
select * from table
whether I like it or not.

I kept trying to modify
      {% for book in results %}
  • {{ book|escape }}
    {% endfor %}
  • into something like:

    {% for book in results %}
  • {{ book|escape }} {{ anothervariable }}
    {% endfor %}

  • Or

    {% for book in results %}
  • {{ book|escape }} {{ modelname.anothervariable }}
    {% endfor %}


  • And I tried all kinds of different things. I tried Waite's suggestion to do some sequential error checking to identify whether it's the url, the view or the template. But assuming it isn't dumb luck, I discovered something that finally broke through to my slow brain.

    I apparently needed this:


    {% for book in results %}
  • {{ book|escape }} {{ book.anothervariable }}
    {% endfor %}

  • Again, assuming that's what I needed (and this isn't luck), I think I've gotten past the big brain block and hopefully Django will be a little easier to digest. I can already see the awesome and amazing speed with which you can put together an app.

    For some reason, I assumed I needed to reference the model name, apparently forgetting the basics so clearly illustrated here in crayon-like colors for dense-minded people like myself.

    That being said, it's cool I've needed to learn so little Python to make something useful (OK, I haven't made anything useful to anyone ELSE yet). As someone who attempts to use Perl, I wasn't looking forward to the idea of learning another scripting language.

    So thanks to all who helped. And thanks to my random late Friday night (I have no life, apparently) find from Jacob Kaplan-Moss.

    No comments: