0

This code creates and adds methods to a class:

def add_controller(self, cls, i, return_url):
    @http.route(return_url, type="http", auth="public", website=True)  # Very important, missing!
    def innercontroller(self, **kw):
        print("Helloo here with ID: " + i)

    innercontroller.__name__ = "controller_html_form_id_" + i  # Name of the new method
    setattr(cls, innercontroller.__name__, innercontroller)
    print("Done")

def create(self):
    self.add_controller(HtmlFormController, i, return_url)

The code works fine and the method is generated, but unfortunately without the decorator. I want to create multiple methods with different return_url, meaning that every decorator will be different.

EDIT:

In the end, I want to have a method like this:

@http.route("/custom13", type="http", auth="public", website=True)
def controller_html_form_id_13(slef, **kw):
    return "Example method!"

So then when the user calls the route example.com/custom13, then he should see Example method! in his browser.

The only thing missing is the decorator @http.route(...)

How can I add this decorator to the generated method?

Alex
  • 491
  • 2
  • 10
  • 25
  • 1
    "But unfortunately without the decorator." why do you say that? – juanpa.arrivillaga Mar 17 '21 at 17:20
  • 1
    This seems like a strange thing to do. Why are you dynamically adding methods who's name differs only by an appended number? What is the original problem you are trying to solve? I think there might be a better solution to **that** problem rather than the question you ask here. – Code-Apprentice Mar 17 '21 at 17:20
  • @Code-Apprentice I want to create new http routes every time the method ```create``` is called. The user enters the url and other data (data which will be then displayed on the new http route) and then the ```add_controller``` method should create this http route. The name of the method is not important at all. – Alex Mar 17 '21 at 17:29
  • 1
    @Alex Why do you need a dynamic routes? Can't you create a single static route? Also, keep in mind that the dynamic routes will only exist in the current process. Flask and other web servers run multiple processes in order to serve multiple requests at the same time. If these routes are created when Flask first initializes the app, then you should be fine, but if these routes are created when processing a request, for example, the route will only exist in the current process, and not in other processes running your Flask app. – Code-Apprentice Mar 17 '21 at 17:31
  • @Code-Apprentice Each route has its own content and multiple routes should be available at the same time. – Alex Mar 17 '21 at 17:36
  • And how do you know the code you have doesn't add the decorator correctly? – Code-Apprentice Mar 17 '21 at 17:38
  • @Code-Apprentice Calling the method by its method name works but calling the method through the route/url does not work (404) – Alex Mar 17 '21 at 17:42
  • It's unclear what you want the decorator to do. Please edit your question and show an example of what the code of a decorated method would look like. – martineau Mar 17 '21 at 17:42
  • @martineau I just edited my question. If it's still unclear, please let me know. Thanks anyway! – Alex Mar 17 '21 at 17:54
  • 4
    "The only thing missing is the decorator" I think this assumption is incorrect. The decorator is clearly in your code. More likely Flask isn't seeing the decorated method. You have to be sure that all your route methods are created before Flask processes them for dispatching. – Code-Apprentice Mar 17 '21 at 17:56
  • The appropriate way to do dynamic routing with flask is to use a variable name enclosed with `< >` in the `return_url` https://stackoverflow.com/questions/35107885/how-to-generate-dynamic-urls-in-flask – Aaron Mar 17 '21 at 18:00

0 Answers0