views:

43

answers:

2

I came across this tutorial:

http://thomas.broxrost.com/2008/04/08/django-on-google-app-engine/

Fantastic!

Everything worked.

I just did not fully understand the code below because in comparison to Django it seems different:

views.py:

def main(request):
    visitor = Visitor()
    visitor.ip = request.META["REMOTE_ADDR"]
    visitor.put()

    result = ""
    visitors = Visitor.all()
    visitors.order("-added_on")

    for visitor in visitors.fetch(limit=40):
        result += visitor.ip + u" visited on " + unicode(visitor.added_on) + u""

    return HttpResponse(result)

#model.py:

from google.appengine.ext import db

class Visitor(db.Model):
    ip = db.StringProperty()
    added_on = db.DateTimeProperty(auto_now_add=True)

What exactly is Visitor() ? A tuple a list?

And what does visitor.ip , visitor.put(), visitors.fetch() do exactly?

I believe:

visitor.ip saves the request.META["REMOTE_ADDR"] in the db field.

visitor.put() saves it.

visitors.fetch(limit = 40) extracts it from the db.

What I was trying to do is a tenplate that lists every IP below the next one.

I believed:

<p><ol><Li> {{ result }} </li></ol></p>

Would do the trick.

But it didn't.

Thanks !

A: 

Without knowing anything about the app engine, I'd say this: Visitor() returns an instance of the Visitor class. The step that follows (visitor.ip = request.META["REMOTE_ADDR"]) sets an attribute of the instance created in the first line.

Manoj Govindan
A: 

Visitor is a class, and each field in it represents a column in your database. When you do visitor = Visitor() you're essentially creating a new row in your database. Calling visitor.put() is what actually commits it into the database. Visitors.all() returns a all the rows in the db (it's either a list, tuple, or dictionary), so then visitors.fetch() is just an operation on that.

The reason your template isn't working is because your function in views.py isn't specifying any template. This is taken from the Django tutorial: http://docs.djangoproject.com/en/1.0/intro/tutorial03/

from django.template import Context, loader
from mysite.polls.models import Poll
from django.http import HttpResponse

def index(request):
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    t = loader.get_template('polls/index.html')
    c = Context({
        'latest_poll_list': latest_poll_list,
    })
    return HttpResponse(t.render(c))

The parameter for Context() is a dictionary. The string on the left is what the name of the variable will be within the template, and the right side is what actual variable that it corresponds to. In your example you can use {'mylist': result}, and in your template you could use {{ mylist }} instead of {{ result }}

You also need to make sure to set a template directory in settings.py which the template (in the above example) is polls/index.html within that template dir.

jonescb
Thanks. It did work. I just did not understand it. But thanks to you, now I understood it.
MacPython