Josh
Josh

Reputation: 5016

How to get started with Sessions in Google App Engine / Django?

I'm new to Python / GAE / Django. I get that with GAE there are no in-memory sessions per se... but I think I want something equivalent. I read that Django sessions can be backed by BigTable or MemCache, but I never got them working. I guess what I'm asking is "Should I..."

  1. Persist with getting Django sessions working?
  2. Look at some other webapp framework for sessions in particular, or the site in general?
  3. Roll my own?

It seems to me that sessions are not supported out-of-the-box and are somehow not first class citizens. What do you do?!

Thanks.

Upvotes: 5

Views: 3025

Answers (4)

Joe Bowman
Joe Bowman

Reputation: 863

gaeuitlities includes a django middleware, however I haven't done django development in a while and can't 100% guarantee it's been kept up to date with django. I'm sure it won't take me long to fix if it there is an issue.

https://github.com/joerussbowman/gaeutilities/blob/master/appengine_utilities/django-middleware/middleware.py

If you use that middleware for you sessions, it should work as you expect including in templates. Please file any issues on github if you encounter problems.

https://github.com/joerussbowman/gaeutilities

Upvotes: 0

Queryer
Queryer

Reputation: 259

I am using gaeutilities session now. However, the problem is that these created sessions are accessible only within the server-side codes. When I try to access them in django template tag, I could retrieve them out. Am I missing something?

Example: Client side (Django template tags)

         {% if request.session["email"]%}
            <p><a href="/logout/"id="menu">Logout</a></p>
            <p class="subtext">GoodBye!</p>
        {% else %}
            <p><a href="/login/"id="menu">Login</a></p>
            <p class="subtext">Welcome!</p>
        {% endif %}

Server side is just a simple self.session['email'] and can be access by all server side files.

Any ideas on how to access them on the client side apart from rendering the session value to the page? I need all client side pages to access the session value.

Upvotes: 1

lprsd
lprsd

Reputation: 87095

The reason django sessions are not supported by App engine out of the box is because django uses database table (model) based sessions, and the django ORM is not supported on appengine.

A solution to this is to make django models work out of the box on appengine. And it has been done by patching django code, in the App Engine Patch project.

Using this patch, as django models work, you get to access django admin, django auth along with the latest django release.

You may also find this blog post on deploying a django application on App engine, useful: http://uswaretech.com/blog/2009/04/develop-twitter-api-application-in-django-and-deploy-on-google-app-engine/

Upvotes: 3

ars
ars

Reputation: 123518

The gaeutilities library comes with a session management class that works well:

Upvotes: 1

Related Questions