Django中reverse()和reverse_lazy()之间的区别 [英] Difference between reverse() and reverse_lazy() in Django
问题描述
我知道我们可以在FBV中使用 reverse()
和在CBV中使用 reverse_lazy()
。我了解我们必须在CBV中使用 reverse_lazy()
,因为导入文件时未加载网址(参考: Reverse_lazy和URL加载?)
我不明白的是:
当我们从FBV调用 reverse
时,如何加载网址?就像我们在Django应用中将 urls.py
顶部的视图导入时一样, urlpatterns
列表尚未被评估。
<$对于FBV而言, reverse()
如何工作? p $ p> #importme.py
def a():
print( FUNCTION HELLO)
class B():
print( CLASS HELLO)
>> import importme
>> CLASS HELLO
在导入时评估类属性。何时或确切如何发生的答案在于python导入系统的深度。
I understand that we can use reverse()
in FBV and reverse_lazy()
in CBV. I understand that we have to use reverse_lazy()
in CBV as the urls are not loaded when the file is imported (Ref: Reverse_lazy and URL Loading?)
What I don't understand is:
How are the urls loaded when we call reverse
from the FBV? As when we import the views at the top of the urls.py
in a Django app, urlpatterns
list is yet to be evaluated. How does reverse()
for FBV work but not for CBV?
#importme.py
def a():
print("FUNCTION HELLO")
class B():
print("CLASS HELLO")
>>> import importme
>>> CLASS HELLO
Class attributes are evaluated on import. The answer to when or exactly how that happens, resides within the depths of python's import system.
这篇关于Django中reverse()和reverse_lazy()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!