本文整理汇总了Python中models.Url.all方法的典型用法代码示例。如果您正苦于以下问题:Python Url.all方法的具体用法?Python Url.all怎么用?Python Url.all使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Url
的用法示例。
在下文中一共展示了Url.all方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from models import Url [as 别名]
# 或者: from models.Url import all [as 别名]
def get(self):
"""Response for GET requests."""
# When rendering the template we pass a dictionary of values that the
# template may use. The templates are rendered in a sandbox so local
# scope is not applicable.
#
# In this instance the only variable is a conditional flag whether a
# short url has been created.
template_values = {
'created': False,
'hostname': os.environ.get('HTTP_HOST'),
'urls': Url.all().order('-timestamp').fetch(limit=10)
}
template = JINJA_ENVIRONMENT.get_template("root.html")
self.response.write(template.render(template_values))
示例2: post
# 需要导入模块: from models import Url [as 别名]
# 或者: from models.Url import all [as 别名]
def post(self):
"""Response for POST requests."""
# The ideas behind a good hashing algorithm are complex. For
# this example we will only truncate the first 10 digits of
# of the md5 hash. This will of course have collisions but it
# is a problem for another day.
m = hashlib.md5()
m.update(self.request.get('link'))
# The unique hash is truncated to keep our links small.
url_hash = m.hexdigest()[0:10]
# A Url model is instinated with the supplied post data. Our url
# shortner uses a hash as a key to recover created urls. To enforce
# link vaildation we try to create an entity with the supplied information
# and anticipate failures.
try:
# By instinating an Url entity we call is validate method which ensures
# the link is good.
url = Url(key_name=url_hash,
link=self.request.get('link'))
except (HTTPError, BadUrlError), e:
# If the underlying URL functions fail in any way we catch the error and
# render the template to include the error message.
template_values = {
'created': False,
'error': e,
'hostname': os.environ.get('HTTP_HOST'),
'urls': Url.all().order('-timestamp').fetch(limit=10)
}
template = JINJA_ENVIRONMENT.get_template("root.html")
self.response.write(template.render(template_values))
示例3: ShortUrlPage
# 需要导入模块: from models import Url [as 别名]
# 或者: from models.Url import all [as 别名]
}
template = JINJA_ENVIRONMENT.get_template("root.html")
self.response.write(template.render(template_values))
else:
# Model.put() commits the model to the datastore.
url.put()
# In this instance the only variable is a conditional flag whether a
# short url has been created.
template_values = {
'created': True,
'hash': url_hash,
'hostname': os.environ.get('HTTP_HOST'),
'urls': Url.all().order('-timestamp').fetch(limit=10)
}
template = JINJA_ENVIRONMENT.get_template("root.html")
self.response.write(template.render(template_values))
class ShortUrlPage(webapp2.RequestHandler):
"""Accepts route requests for anything but root."""
# Requests to a short url should only accept GET http methods.
def get(self, hash):
# The request parses the hash from the path by slicing the string to omit
# the leading "\". We ask the datastore for any entity that matches the hash.
url = Url.get_by_key_name(hash)
# If the hash is valid it will return Url Model object else it will return None