google-app-engine Quick start with Users Python API, App Engine Authentication MainPage Handler [views.py]

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

General Imports, using jinja2 to populate templates into htmls.

import jinja2
import webapp2

Important import to use Users API:

from google.appengine.api import users

Setting of Jinja environment: [into the example the tehcnology selected to populate the information into the frontend]

JINJA_ENVIRONMENT = jinja2.Environment(
       loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
       extensions=['jinja2.ext.autoescape'],
       autoescape=True)

Concrete Handler:

class MainPage(webapp2.RequestHandler):
   def get(self):

       user = users.get_current_user()
       if user:
           url = users.create_logout_url(self.request.uri)

You can include more logic here for users

       else:
           url = users.create_login_url(self.request.uri)

Templates to pass information using jinja2. For this example, the user object and the url string.

       template_values = {
           'user': user,
           'url': url,
       }

       JINJA_ENVIRONMENT.add_extension('jinja2.ext.do')

Using index.html example. [traditional html page]

       template = JINJA_ENVIRONMENT.get_template('index.html')
       self.response.write(template.render(template_values))


Got any google-app-engine Question?