I am learning GAE with python. I am trying to build the simplest possible application: get name from user; write name to datastore; retrieve name and display page. I tried the tutorial but I still do not understand how to do this. I appreciate any answers. Thank you
Hi, i´m going to post a little snippet: Create a file in your root directory, name it main.py
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
# Pagina principal
class MainPage(webapp.RequestHandler):
def get(self):
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = "Bem Vindo: "+ str(users.get_current_user()) + ". Logout "
else:
url = users.create_login_url(self.request.uri)
url_linktext = ' Entrar '
values = {
'url': url,
'url_linktext': url_linktext,
}
self.response.out.write(template.render('templates/index.html', values))
application = webapp.WSGIApplication([
('/', MainPage),
],debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
then create a folder in your root directory, name it templates. Inside templates create a file and name it base.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="styles/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="mainContainer">
<!--menu-->
<div id="menu">
<ul id="menuUl">
<li class="selected"><a href="/"> Inicio </a> </li>
<li><a href="#">Sobre</a></li>
<li><a href="#">Else</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Contacto</a></li>
</ul>
</div>
<!--End menu-->
</div>
<hr />
<!--End Navigation-->
<div id="header" >
{% block header %} {% endblock %}
</div>
<div id="contentContainer">
<!--content-->
<div id="content">
{% block main %} {% endblock %}
</div>
<div id="contentBottom" >
<div id="contentBottomLeft"></div>
<div id="contentBottomRight"></div>
</div>
</div>
<div id="footer">
<div id="footerMenu">
<ul>
<li class="selected"><a href="/"> Inicio </a> </li>
<li><a href="#">Sobre</a></li>
<li><a href="#">Ipca</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Contacto</a></li>
</ul>
</div>
<p>Copyright © 2010 Martin . Todos os direitos reservados.</p>
</div>
</div>
</div>
</body>
</html>
This page is static. This code {% block main %} {% endblock %} and this {% block header %} {% endblock %} This code represents a variable that will receive a template. So if you want to put content on the header and content, you must create a new file, let's call index.html.
{% extends "base.html" %}
{% block header %}
<div class="hello">
<a href="{{ url }}">{{ url_linktext }}</a>
</div>
{% endblock %}
{% block main %}
<h1>Um pouco de palha</h1>
<p class="smallSubtitle">Isto e mais palha .......</p>
{% endblock %}
When you create a new template file, you have to put this code {% extends "base.html" %} and then you´ll call the block from header and content and fill it up.
What this does is present a page with Login info in it´s header. If you haven´t login yet it redirects you to login, else (it means, you´ve already login) shows the Logout button. Then presents dummy content on content block