I also wrote middleware for SQLAlchemy--I'd post it, but it depends on other libraries that I wrote that I can't really share. What I found is that, at least while using the dev server, the process_response method would get called when serving media files, even if the process_request hadn't. So, simple solution: wrap the session_destroy call in a try/except block. Here's mine:
def process_response(self, request, response): try: request.db_session.commit() request.db_session.close() except AttributeError: pass return response Also, may I suggest adding a process_exception method? Here's mine: def process_exception(self, request, exception): request.db_session.rollback() request.db_session.close() -Jeff On Nov 12, 7:18 am, ershadul <[EMAIL PROTECTED]> wrote: > Dear all, > please consider the following code-block: > > class SQLAlchemySessionMiddleware(object): > """ > This class instantiates a sqlalchemy session and destroys > """ > def process_request(self, request): > request.db_session = session() > return None > > def process_response(self, request, response): > session_destroy(request.db_session) > return response > > Consider that session() is method that returns a new object sqlalchemy > session. > I want to inject it before view is processed and destroy it upon > exiting the view. > > I have added the path of this middleware class before XView > middleware in settings.py. > But i got a error that > request has not attribute 'db_session' ( process_response function) > How can i do it? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---