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 6845035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:27:02+00:00 2026-05-27T00:27:02+00:00

I am writing my website’s backend using Flask and Python 2.7, and have run

  • 0

I am writing my website’s backend using Flask and Python 2.7, and have run into a bit of a problem. I like to use classes to enclose my functions, it makes things neat for me and helps me keep everything modular. One problem I’m having, though, is that the decorators flask uses for routing doesn’t preserve the self variable. I use this for accessing the loadDb method of the class that its in. See below. Anyone have any ideas why this is happening, and know how I could solve this, or even if there is a way to solve this?

class Test(object):
    blueprint = Blueprint("Test", __name__)
    def __init__(self, db_host, db_port):
        self.db_host = db_host
        self.db_port = db_port
    def loadDb(self):
        return Connection(self.db_host, self.db_port)
    @blueprint.route("/<var>")
    def testView(var): # adding self here gives me an error
        return render_template("base.html", myvar=self.loadDb().find({"id": var})
  • 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-05-27T00:27:03+00:00Added an answer on May 27, 2026 at 12:27 am

    There is an error if you add self because the method works the same as a function for the decorator, and the flask isn’t expecting a function with a first argument self.

    Let’s look at the code of route : https://github.com/pallets/flask/blob/master/src/flask/blueprints.py#L52

    It calls self.add_url_rule (self being the Blueprint) with a few arguments, one of those being the function. What you want is to add a rule with the method bound to an instance of Test(self.testView), not the method itself (Test.testview). This is tricky because the decorator is executed at the creation of the class, before any instance ever exist.

    The solution I can suggest, asside from avoiding to put your views as methods of a class, is to call yourself blueprint.add_url_rule in the contructor of Test (i.e., at the first point the instance of Test is known.

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

Sidebar

Related Questions

I'm writing multilingual website. I have several files on server like: /index.php /files.php /funny.php
I'm writing a website using JSP. I want to have the website available in
I am writing a website using Python / Django, and am currently doing server-side
I'm writing a website/iPad app (using PhoneGap ), where I have 1024x768 images on
I'm writing a MVC website and using jqGrid. I have managed to get it
I am writing website and i`d like to implement profile managment. Basic thing would
I'm writing a website that will sell items from one of my classes. It
I am writing a website with Visual Studio 2008 and ASP.NET 3.5. I have
I'm writing a website in JSP using Struts and Hibernate. I'm looking for a
I'm currently writing a website in ASP.NET MVC, and my database (which doesn't have

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.