从函数django中终止端点

发布于 2025-02-07 11:19:59 字数 251 浏览 1 评论 0原文

有没有办法从功能内部结束端点?我知道我可以使该函数返回真或错误,然后评估答案并返回,但是如果我可以做这样的事情,那将更加干净:

def custom_endpoint(self, request):
    check_something(request)
    return Response("ok")

check_something(请求)应检查变量,然后结束端点的执行如果一个变量不正确。

谢谢!!

Is there a way to end an endpoint from inside a function? I know I can make the function to return True or False, then evaluate that answer and return, but it would be a lot cleaner if I can do something like this:

def custom_endpoint(self, request):
    check_something(request)
    return Response("ok")

check_something(request) should check variables and then end the execution of the endpoint if one of the variables is not correct.

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2025-02-14 11:19:59

您的公用事业功能只能返回其呼叫者,即您的视图功能。您必须以某种方式使用实用程序函数的返回值来结束视图功能的执行。您的视图函数必须返回Web响应。

当然,您还可以使用错误处理机制,而不是在发生错误时使用返回值。

还将讨论在这里

Your utility function can only return to its caller, i.e. your view function. You must somehow use the return value of your utility function to end the execution of your view function. Your view function must return a web response.

Of course, you can also use an error-handling mechanism instead of using return values in case of errors.

This is also discussed here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文