Django文档展示了如何通过捕获EmptyPage异常使用基于函数的视图返回分页查询集的最后一页。
例如,使用基于类的通用视图实现相同目标的最简单方法是什么ListView?
我首先认为allow_emptyfor的设置MultipleObjectMixin可以满足我的需要,但检查代码表明,如果查询集中的对象为零,而不是请求的页面上的对象为零,它只会防止 404 错误。
两个选项似乎是:
ListView和覆盖paginate_queryset(继承自MultipleObjectMixin),或Paginator和 override validate_number,并paginator_class在视图中设置为子类。有没有更好的方法来实现这一目标?
我正在尝试使用 Python 的inspect模块(在 Python 2 中)来显示有关调用当前函数的函数的信息,包括其参数。
这是一个简单的测试程序:
import inspect
def caller_args():
frame = inspect.currentframe()
outer_frames = inspect.getouterframes(frame)
caller_frame = outer_frames[1]
return inspect.getargvalues(caller_frame)
def fun_a(arg1):
print caller_args()
def fun_b():
fun_a('foo')
if __name__ == '__main__':
fun_b()
Run Code Online (Sandbox Code Playgroud)
当我运行它时会发生这种情况:
$ python getargvalues_test.py
Traceback (most recent call last):
File "getargvalues_test.py", line 16, in <module>
fun_b()
File "getargvalues_test.py", line 13, in fun_b
fun_a('foo')
File "getargvalues_test.py", line 10, in fun_a
print caller_args()
File "getargvalues_test.py", line 7, in caller_args
return inspect.getargvalues(caller_frame)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 829, in …Run Code Online (Sandbox Code Playgroud)