Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9061981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:37:17+00:00 2026-06-16T15:37:17+00:00

I have a flask application that is laid out as a few MethodView s.

  • 0

I have a flask application that is laid out as a few MethodViews.

On one of my MethodViews, I want to open a database connection before each request, and close it after the request, very similar to this.

I know how to use global @app.before_request and @app.teardown_request functions, but those will run for every single request. I want limited versions that will only run for the routes in a particular MethodView.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:37:18+00:00Added an answer on June 16, 2026 at 3:37 pm

    You could use a decorator, that would tag certain views as database dependent.

    NoSQLStore = {}
    
    class NoSQL(object):
        """ fake """
        def query(self, key): 
            return '%s\n' % NoSQLStore.get(key, 'no such key')
        def put(self, key, value):
            NoSQLStore[key] = value
    
    def with_database(fn):
        """ Decorator for functions, that need database access.
            NoSQL object will be stored under ``g.db``. 
        """
        def connect_and_close(*args, **kwargs):
            g.db = NoSQL()
            __result = fn(*args, **kwargs)
            # a real database should be somehow ``closed()`` here
            return __result
        return connect_and_close
    
    class StoreAPI(MethodView):
    
        @with_database
        def get(self):
            return g.db.query(request.args.get('key'))
    
        @with_database
        def post(self):
            key, value = str(random.randint(0, 1000)), str(random.randint(0, 1000))
            g.db.put(key, value)
            return 'set %s => %s\n' % (key, value)
    

    A runnable standalone example can be found here: https://gist.github.com/4424587

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a flask application that is setting up a database connection in a
I have a weird error in my flask application. Problem is that the code
I have this flash application that opens a socket connection with a server. What
I have an application that will use flask and mongodb; I will probably host
I have a Flask application that uses WTForms for user input. It uses a
I have a flash map application that I want to rebuild on the new
I have an projector file/Flash application that I need to turn into an interactive
Imagine that I have a form in a flash application with two fields, input1
I have a web application which is deployed by the means of Flask. I'm
I have a flash application written in action script 2, and at one point

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.