Posts

Showing posts with the label session

Set the default session expired time in Django

In Django, you can set the expired time of session by this setting, for example after 10 minutes: SESSION_COOKIE_AGE = 10 * 60 In addition, if you want the session will be expired when the user closes her browser, you can set: SESSION_EXPIRE_AT_BROWSER_CLOSE = True

To make Atom remember your last session

To enable Atom to remember your last session whenever you re-open it, install this plugin Github repo:  https://github.com/mpeterson2/save-session >> Edit >> Preferences >> Install >> search for "save-session", install. Close Atom, and reopen it to see the magic.

SQLAlchemy - Get and delete object

These are some utility methods to make a query with SQLAlchemy: I gonna use the MyDatabaseConnector class from the previous blog post : db =  MyDatabaseConnector () * Get object: def get _obj (db, pk): session = db.get_session() try: obj = session.query(MyObjModel).get(pk) except: print "Object not found" finally: session.commit() db.close() * Delete object: def delete_obj (db, pk): session = db.get_session() try: obj = session.query(MyObjModel).get(pk) except: print "Object not found" else: session.delete(obj) finally: session.commit() db.close() Reference:   http://docs.sqlalchemy.org/en/rel_0_9/orm/query.html

SQLAlchemy - DetachedInstanceError

I got this error when trying to retrieve all the database records in a table: DetachedInstanceError: Instance <CoursesMerger at 0x37fa950> is not bound to a Session; attribute refresh operation cannot proceed and this is how I query the database: rs = session.query(MyTable).all() To avoid this error, remember to set expire_on_commit = False when initialize the session: from sqlalchemy import * from sqlalchemy.orm import * DB_HOST = 'localhost' DBMS = 'mysql' DB_PORT = '3306' DB_NAME = 'mydatabase' DB_USER = 'myuser' DB_PWD = 'mypassword' DB_ENGINE_URL = '%s://%s:%s@%s:%s/%s' % (DBMS, DB_USER, DB_PWD, DB_HOST,  DB_PORT, DB_NAME) class MyDatabaseConnector (object): engine = None Session = None def __init__ (self): self.connect() def connect (self): if self.engine is None: self.engine = create_engine(DB_ENGINE_URL, echo=False, \ pool_size = 100, pool_recycle=3600) if se...

SQLAlchemy - MySQL has gone away

In the previous blog post, I showed you how to use Django's Form with SQLAlchemy to manipulate MySQL db . There's something annoying me a lot after the day I post that article,  that's is the error: "MySQL has gone away" What's the...? I refreshed the page after an idle time, and the error appears from nowhere?! And if I refresh again, the app will load beautifully. What's going on here? It seams like that is the common error when working with SQLAlchemy, and If you don't manage the session carefully and appropriately. The most common reason is the SQLAlchemy's session is still open while the connection to MySQL server timing out. So here are what I have to do with my_db_utils.py:

Running multiple Django Projects on a same machine

You only need to do 2 things: 1. Run those Django projects on different ports 2. Set different session cookie names for them . Example: - Project1: settings.py: SESSION_COOKIE_NAME = 'project1' - Project2: settings.py: SESSION_COOKIE_NAME = 'project2' In addition, if your Django servers are under a nginx reverse proxy: