简体   繁体   English

request.endpoint[:5] 的内容

[英]Contents of request.endpoint[:5]

I'm going through Miguel Grinbergs Flask book and at one point he uses the following line:我正在阅读 Miguel Grinbergs Flask 的书,有一次他使用了以下行:

request.endpoint[:5] != 'auth.'

I know [:5] is a slice operation, but I'm not sure why it is being used here.我知道[:5]是一个切片操作,但我不确定为什么在这里使用它。 What does the list consist of that we only want elements 0-5?列表由什么组成,我们只想要元素 0-5?

What does the list consist of that we only want elements 0-5?列表由什么组成,我们只想要元素 0-5?

To be precise, request.endpoint is not a list, it's a string.准确地说, request.endpoint不是一个列表,它是一个字符串。 And it doesn't matter what the rest of it contains, the code is only concerned with it beginning with 'auth.'它的其余部分包含什么并不重要,代码只关心它以'auth.'开头'auth.' :

('auth.somethingsomething'[:5] == 'auth.') is True

request.endpoint is the name the current view function was registered as, for example auth.login is the name of the def login(): view. request.endpoint是当前视图函数注册的名称,例如auth.logindef login():视图的名称。 Views that have a prefix like prefix.具有类似prefix.视图prefix. were registered on a blueprint, which groups related views.已注册在蓝图上,该蓝图将相关视图分组。 So the code is checking if the current view being handled is part of the auth blueprint.所以代码正在检查正在处理的当前视图是否是auth蓝图的一部分。

If you're curious about what value it contains, you can add a debugging breakpoint to the code and inspect it:如果您对它包含的值感到好奇,可以在代码中添加调试断点并检查它:

# ... previous app code ...
import pdb; pdb.set_trace()
request.endpoint[:5] != 'auth.'

Then run and test the code.然后运行并测试代码。 When it hits that point, it'll pause execution and give you a pdb shell, which will let you look at the request object and its endpoint attribute.当它到达那个点时,它会暂停执行并为您提供一个pdb shell,它可以让您查看request对象及其endpoint属性。

you can checking on terminal by您可以通过以下方式检查终端

venv $ python manage.py shell
import flask from request
print(request.endpoint)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM