在vi中调用的函数中完成即时脚本

2024-03-28 16:37:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道如何用一句话来解释我需要什么,但是让我们看看PHP中的示例。你知道吗

function sth() { exit(); }

echo 'you can see this';

sth();

echo 'this won't be shown';

?>

换句话说,在PHP中,我可以定义一个函数,在这个函数中,可以使用exit()立即停止脚本的执行

Django呢?你知道吗

def sth():

# what to write here so that script will be stopped?

pass

def some_view(request):

print "oh well, this will be seen either in the error log or the console."

sth()

print "this will not be printed"


Tags: the函数echoyou示例defexitfunction
1条回答
网友
1楼 · 发布于 2024-03-28 16:37:07

您可以提前从视图返回:

def some_view(request):
    # ...
    return direct_to_template(...)
    # this code is unreachable

要通过嵌套调用实现这一点,需要引发异常并从视图中处理它。但不要这样做,绝对没有必要这样做。如果您有不可访问的代码,那么应该完全删除它,而不是试图发明解决方法。你知道吗

相关问题 更多 >