votes up 4

'view_func' name may not contain a dot '.' character.

Package:
flask
github stars 56479
Exception Class:
ValueError

Raise code

        """Like :meth:`Flask.add_url_rule` but for a blueprint.  The endpoint for
        the :func:`url_for` function is prefixed with the name of the blueprint.
        """
        if endpoint and "." in endpoint:
            raise ValueError("'endpoint' may not contain a dot '.' character.")

        if view_func and hasattr(view_func, "__name__") and "." in view_func.__name__:
            raise ValueError("'view_func' name may not contain a dot '.' character.")

        self.record(lambda s: s.add_url_rule(rule, endpoint, view_func, **options))

    def app_template_filter(self, name: t.Optional[str] = None) -> t.Callable:
        """Register a custom template filter, available application wide.  Like
        :meth:`Flask.template_filter` but for a blueprint.
 """
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 0 votes down

Code to reproduce:

from flask import Blueprint
from flask.views import View

blueprint = Blueprint('blueprint', __name__)

class Test(View):
    methods = ['GET']

    def dispatch_request(self, name):
        return 'Welcome %s!' % name

blueprint.add_url_rule('/test', view_func=Test.as_view('test.')) # notice view_func is passed a view name 'test.' which contains a dot

The above code raised the following exception:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-2-681e8febef95> in <module>()
     10         return 'Welcome %s!' % name
     11 
---> 12 blueprint.add_url_rule('/test',view_func=Test.as_view('test.'))

/usr/local/lib/python3.7/dist-packages/flask/blueprints.py in add_url_rule(self, rule, endpoint, view_func, **options)
    291             assert (
    292                 "." not in view_func.__name__
--> 293             ), "Blueprint view function name should not contain dots"
    294         self.record(lambda s: s.add_url_rule(rule, endpoint, view_func, **options))
    295 

AssertionError: Blueprint view function name should not contain dots

Fixed version of code:

from flask import Blueprint
from flask.views import View

blueprint = Blueprint('blueprint', __name__)

class Test(View):
    methods = ['GET']

    def dispatch_request(self, name):
        return 'Hello %s!' % name

blueprint.add_url_rule('/test', view_func=Test.as_view('test'))  # No exception since the view function name doesn't have a dot. 

Summary:

While registering a URL rule along with a view function to the flask blueprint, the exception is raised when we provide a view function name that contains a dot. To reproduce the exception we provided a view function name as 'test.' and we were able to successfully raise the exception. To fix it we must ensure that there are no dots in the view function name for the flask blueprint.

Jun 18, 2021 umangtaneja98 answer

Add a possible fix

Please authorize to post fix