I'm working on an ecommerce website, using Google App Engine with Python. Now, since its an ecommerce website, we would be having dozens of products displayed, each having its own webpage. Now, my question is, if we have about 400 web pages on our server, won't it make the site pretty heavy and bulky? Won't that affect the user experience?
How can we manage the 400-odd web pages on Google App Engine? Is there something I don't know about making a web application less bulky in spite of hosting multiple web pages on the server?
You can use webapp2 framework and fetch product info from the datastore and render it with a template. You make an entity for your product e.g.
class Product(db.Model):
tags = db.ListProperty(db.Category)
category = db.CategoryProperty(verbose_name='Category')
title = db.StringProperty(verbose_name='title') # required
text = db.TextProperty(verbose_name='text')
Then you have a handler class using webapp2, flask, bottle or similar to fetch your product data and render it with for instance the jinja 2 templating engine.
class ProductHandler(BaseHandler):
def get_product(self, key):
data = memcache.get(key)
if data is not None:
return data
else:
data = Product.get_by_id(long(key))
memcache.add(key, data, 6000)
return data
def get(self, id, html):
product= self.get_product(id)
if not product:
self.error(404)
return
self.render_jinja(
'view_product',
product=product)
Then in your template you can use the variables on the view_product.html e.g.
{{product.title}
And the routing is done with a config object for webapp2 (this will be different if you use flask or bottle but if you use flask or bottle you can't leverage webapp2's extras such as User models and i18n)
app = webapp2.WSGIApplication([('/view/(\d+)(\.html?)?', ProductHandler),
Related
I want to create some entries in my database, and then render them via jinja2.
The following is my execution flow:
app = webapp2.WSGIApplication([ ('/dummy', createDummy),('/', MainPage)],debug=True)
where createDummy is:
class createDummy(webapp2.RequestHandler):
def post(self):
print("Hi! Let's post some data to db")
book1 = BooksPost(bookname="abcd",
authorname="xyz")
book1.put()
self.redirect('/')**
However, it is not going into createDummy, but is directly going to the MainPage...what could be the possible reason?
I am building a simple site using python, jinja2 and specifically on one page I'm using angularjs. I'm relatively new to jinja2 and angular. I need help posting to the GAE datastore and displaying the comment dynamically without reloading the page (angular). Here's handler part in the main.py:
class expHandler(webapp2.RequestHandler):
def get(self):
title="Colin_MK: Experience"
recommendations = Recommendation.query()
self.response.out.write(json.dumps([rec.to_dict() for rec in recommendations]))
template_vars = {'title': title, 'recommendations': recommendations}
template = JINJA_ENVIRONMENT.get_template('/exp.html')
self.response.out.write(template.render(template_vars))
def post(self):
r = json.loads(self.request.body)
new_comment = Recommendation(comment=r['comment'])
new_comment.put()
Is it possible to do url rewrite with Flask under Nginx+uWSGI?
I need to add SEO links, so pages of Flask website should be available with two links, for example:
/post/3 and /2014_10_08_post_title.
Connections between normal link and SEO link should be stored in database.
What is the easist way to do it? Is it better and faster way to do it within Flask app or it can be done within nginx?
Thanks!
Flask allows routing multiple URLs to the same view:
#route('/post/<post_id>', defaults={'seo_url': None})
#route('/<seo_url>', defaults={'post_id': None})
def view(post_id, seo_url):
if post_id:
...
elif seo_url:
...
I have been developing on the GAE dev_appserver and my code relied heavily on Django's transactionmiddleware. I have tested it locally and it works.
After deployment to GAE, however, model saves that are committed are not rolled back.
Sample code:
#transaction.commit_on_success
def get(self, request):
name = request.GET.get('name')
d = Department(name=name)
d.save()
raise Exception('Failed')
Is this because Django transaction API is not honored by GAE or is it a problem on my app settings?
FYI django.middleware.transaction.TransactionMiddleware is currently last on the list of MIDDLEWARE_CLASSES
According to this website, the Django database backend for Google App Engine does not support Django transactions. You can however use the run_in_transaction method from the App Engine's SDK.
My application is on GAE and I'm trying to figure out how to prevent hotlinking of images dynamically served (e.g. /image?id=E23432E) in Python. Please advise.
In Google webapp framework, you can extract the referer from the Request class:
def get(self):
referer = self.request.headers.get("Referer")
# Will be None if no referer given in header.
Note that's referer, not referrer (see this dictionary entry).