Skip to content

Blueprint view function name should not contain dots #2790

@gdude2002

Description

@gdude2002

I've done some homework and read up on #2450. I don't really understand the rationale behind that PR, and I don't think it really helps with Flask usage overall. To explain a bit...

I maintain a relatively complex project that builds its own API on top of Flask. This change heavily breaks our application structure, as we currently use dots as separators for view classes under our blueprints. Consider the following:

  • You have sets of blueprints - we use one for each subdomain
  • Each blueprint has a name - for example, main
  • Under a blueprint might be a logical section - for example, there might be an "about" section, so let's call this main.about
  • The logical section will have pages under it - to me, the logical thing to do is use more dots as separators, so main.about.privacy for example

Now, one solution would be to use some other separator. Why not about/privacy? about-privacy? The thing is, regardless of what separator you decide to use there, blueprints must always be separated from the view name using a .. So in, url_for() for example, we would have to use url_for("main.about/privacy"), which just looks plain ugly.

Some sample code A sample route:
from pysite.base_route import TemplateView

class PrivacyView(TemplateView):
    path = "/about/privacy"
    name = "about.privacy"
    template = "main/about/privacy.html"

How we register this route:

class TemplateView(RouteView):
    name = None  # type: str
    path = None  # type: str

    # We have a bunch of inheritance, but here's the relevant method from the superclass.
    # RouteView inherits from MethodView and we dynamically load all our routes on startup.

    @classmethod
    def setup(cls: "RouteView", manager: "pysite.route_manager.RouteManager", blueprint: Blueprint):
        """
        Set up the view by adding it to the blueprint passed in - this will also deal with multiple inheritance by
        calling `super().setup()` as appropriate.

        This is for a standard route view. Nothing special here.

        :param manager: Instance of the current RouteManager
        :param blueprint: Current Flask blueprint to register this route to
        """

        # ... misc code

        blueprint.add_url_rule(cls.path, view_func=cls.as_view(cls.name))

This seems like the correct way to be doing this, right?


So, to summarize, here's what I'm asking:

  1. Why exactly was this change made in the first place?
  2. What should we be doing otherwise?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions