Method: flask.ctx.has_request_context
Calls: 2, Exceptions: 0, Paths: 1Back
Path 1: 2 calls (1.0)
False (1) True (1)
1def has_request_context() -> bool:
2 """If you have code that wants to test if a request context is there or
3 not this function can be used. For instance, you may want to take advantage
4 of request information if the request object is available, but fail
5 silently if it is unavailable.
6
7 ::
8
9 class User(db.Model):
10
11 def __init__(self, username, remote_addr=None):
12 self.username = username
13 if remote_addr is None and has_request_context():
14 remote_addr = request.remote_addr
15 self.remote_addr = remote_addr
16
17 Alternatively you can also just test any of the context bound objects
18 (such as :class:`request` or :class:`g`) for truthness::
19
20 class User(db.Model):
21
22 def __init__(self, username, remote_addr=None):
23 self.username = username
24 if remote_addr is None and request:
25 remote_addr = request.remote_addr
26 self.remote_addr = remote_addr
27
28 .. versionadded:: 0.7
29 """
30 return _cv_request.get(None) is not None