So I understand here that python is assigning the render variable to web.template.render, but I don’t really get what it’s doing in english. My templates directory is being “called” (is this the right word?) and the layout.html is used in some way. I’ve been trying to break the code and it still works without this line though. I’ve been instructed to use this and don’t understand it.
render = web.template.render('templates/', base="layout")
The following class makes sense to me to this extent: an object assigned to it will return hello_form.html and because of the second function (method?) it will allow a user to input things into a prompt and return those values. I don’t understand very deeply what the form variable line or the return line are doing though. Any help or quick translations will help me greatly, thanks!
class Index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(name = "what", greet = "no response?")
greeting = "%s, %s" % (form.greet, form.name)
return render.index(greeting = greeting)
Correct me if I’m wrong but your confusion is the
name = "what"type syntax? This just says that the function, in this caseweb.inputtakes a parameter callednameand you are assigning it directly to"what". Same withrender.index(greeting = greeting), it takes a parametergreetingto which you assign the value of the local variablegreetingwhich was evaluated in the line above thereturnstatement.So without looking at the method, I would say there is no guarantee that
return render.index(greeting)is the same asrender.index(greeting = greeting). Take for example this snippet of code taken from here, but without the infinite loop I just noticed:You could then call this function this way:
Where in the last line we did not specify
n, the first parameter, but did specifytxt. I also agree with you thatgreeting = greetingis confusing. It seems like a cute little trick, but I don’t much care for it. The parameter name isgreeting, liketxtin our example, and the local variable isgreeting. Personally, I would have named the local variable something else and then had,render.index(greeting = myGreetingText)or something like that.