add blueprint name to request.endpoint
This commit is contained in:
parent
75f2180cb1
commit
9150767574
|
@ -136,3 +136,24 @@ app = Sanic()
|
|||
def hello(request):
|
||||
return text(request.endpoint)
|
||||
```
|
||||
|
||||
Or, with a blueprint it will be include both, separated by a period. For example,
|
||||
the below route would return foo.bar:
|
||||
|
||||
```python
|
||||
from sanic import Sanic
|
||||
from sanic import Blueprint
|
||||
from sanic.response import text
|
||||
|
||||
|
||||
app = Sanic(__name__)
|
||||
blueprint = Blueprint('foo')
|
||||
|
||||
@blueprint.get('/')
|
||||
async def bar(request):
|
||||
return text(request.endpoint)
|
||||
|
||||
app.blueprint(blueprint)
|
||||
|
||||
app.run(host="0.0.0.0", port=8000, debug=True)
|
||||
```
|
||||
|
|
14
sanic/app.py
14
sanic/app.py
|
@ -247,7 +247,12 @@ class Sanic:
|
|||
def response(handler):
|
||||
async def websocket_handler(request, *args, **kwargs):
|
||||
request.app = self
|
||||
request.endpoint = handler.__name__
|
||||
if not getattr(handler, '__blueprintname__', False):
|
||||
request.endpoint = handler.__name__
|
||||
else:
|
||||
request.endpoint = getattr(handler,
|
||||
'__blueprintname__',
|
||||
'') + handler.__name__
|
||||
try:
|
||||
protocol = request.transport.get_protocol()
|
||||
except AttributeError:
|
||||
|
@ -541,7 +546,12 @@ class Sanic:
|
|||
|
||||
# Fetch handler from router
|
||||
handler, args, kwargs, uri = self.router.get(request)
|
||||
request.endpoint = handler.__name__
|
||||
if not getattr(handler, '__blueprintname__', False):
|
||||
request.endpoint = handler.__name__
|
||||
else:
|
||||
request.endpoint = getattr(handler,
|
||||
'__blueprintname__',
|
||||
'') + handler.__name__
|
||||
request.uri_template = uri
|
||||
if handler is None:
|
||||
raise ServerError(
|
||||
|
|
Loading…
Reference in New Issue
Block a user