Django 中 reverse() 和 reverse_lazy() 的区别
Difference between reverse() and reverse_lazy() in Django
我知道我们可以在 FBV 中使用 reverse()
,在 CBV 中使用 reverse_lazy()
。我知道我们必须在 CBV 中使用 reverse_lazy()
,因为导入文件时不会加载 url(参考:Reverse_lazy and URL Loading?)
我不明白的是:
当我们从 FBV 调用 reverse
时,如何加载 url?当我们在 Django 应用程序中导入 urls.py
顶部的视图时,urlpatterns
列表尚未评估。 reverse()
对 FBV 但对 CBV 不起作用?
#importme.py
def a():
print("FUNCTION HELLO")
class B():
print("CLASS HELLO")
>>> import importme
>>> CLASS HELLO
编辑:
原因:class 创建过程涉及执行 class.
的主体
The class body is executed (approximately) as exec(body, globals(), namespace)
. [...] Once the class namespace has been populated by
executing the class body, the class object is created by calling
metaclass(name, bases, namespace, **kwds)
.
https://docs.python.org/3/reference/datamodel.html?highlight=metaclass#executing-the-class-body
我原来的回答文字。你可以忽略它——我只是把它留在原处,因为 mirek 的评论是对它的直接回应:
Class 属性在导入时进行评估。何时或如何发生的答案存在于 python 导入系统的深处。
考虑这两种定义 success_url 的方法。第一个被注释掉,
第二个是函数:
class NewJobCBV(LoginRequiredMixin, CreateView):
template_name = 'company/job.html'
form_class = newJobForm
# success_url = reverse_lazy('newJob')
def get_success_url(self, **kwargs):
return reverse("newJob")
@CoffeeBasedLifeform:您说得对,class 属性在导入时进行评估,我在阅读您的回答后进行了检查。所以,
- 如果我们使用
success_url
,我们必须使用 reverse_lazy()
。
- 如果我们在函数内部反转,我们可以使用
reverse()
.
现在 crystal 清楚了。
谢谢 CoffeeBasedLifeform :)
因为:Python class 属性是在声明中计算的。
请阅读此 link:Python class attributes are...
了解一下区别:
reverse() returns a string
& reverse_lazy() returns一个<object>
顾名思义,Reverse_lazy 是 reverse[=] 的 lazy 实现 21=] URL 解析器。与传统的反向函数不同,reverse_lazy 在需要值之前不会执行。
它很有用,因为它可以防止 Reverse Not Found 在处理可能无法立即知道的 URL 时出现异常。
我们为什么需要它?
这是必需的,因为 Class 属性在导入时进行评估,那时反向方法将 return 'Reverse Not Found'。
稍后根据需要,在执行时,所有必要的代码片段都将被执行,以提供有效的 URL.
区别:
- reverse() 在函数中使用 & reverse_lazy() 在 class.
中使用
- reverse() 在字符串中使用 & reverse_lazy() 在对象中使用
我知道我们可以在 FBV 中使用 reverse()
,在 CBV 中使用 reverse_lazy()
。我知道我们必须在 CBV 中使用 reverse_lazy()
,因为导入文件时不会加载 url(参考:Reverse_lazy and URL Loading?)
我不明白的是:
当我们从 FBV 调用 reverse
时,如何加载 url?当我们在 Django 应用程序中导入 urls.py
顶部的视图时,urlpatterns
列表尚未评估。 reverse()
对 FBV 但对 CBV 不起作用?
#importme.py
def a():
print("FUNCTION HELLO")
class B():
print("CLASS HELLO")
>>> import importme
>>> CLASS HELLO
编辑: 原因:class 创建过程涉及执行 class.
的主体The class body is executed (approximately) as
exec(body, globals(), namespace)
. [...] Once the class namespace has been populated by executing the class body, the class object is created by callingmetaclass(name, bases, namespace, **kwds)
.
https://docs.python.org/3/reference/datamodel.html?highlight=metaclass#executing-the-class-body
我原来的回答文字。你可以忽略它——我只是把它留在原处,因为 mirek 的评论是对它的直接回应:
Class 属性在导入时进行评估。何时或如何发生的答案存在于 python 导入系统的深处。
考虑这两种定义 success_url 的方法。第一个被注释掉, 第二个是函数:
class NewJobCBV(LoginRequiredMixin, CreateView):
template_name = 'company/job.html'
form_class = newJobForm
# success_url = reverse_lazy('newJob')
def get_success_url(self, **kwargs):
return reverse("newJob")
@CoffeeBasedLifeform:您说得对,class 属性在导入时进行评估,我在阅读您的回答后进行了检查。所以,
- 如果我们使用
success_url
,我们必须使用reverse_lazy()
。 - 如果我们在函数内部反转,我们可以使用
reverse()
.
现在 crystal 清楚了。
谢谢 CoffeeBasedLifeform :)
因为:Python class 属性是在声明中计算的。 请阅读此 link:Python class attributes are...
了解一下区别:
reverse() returns a string
& reverse_lazy() returns一个<object>
Reverse_lazy 是 reverse[=] 的 lazy 实现 21=] URL 解析器。与传统的反向函数不同,reverse_lazy 在需要值之前不会执行。
它很有用,因为它可以防止 Reverse Not Found 在处理可能无法立即知道的 URL 时出现异常。
我们为什么需要它? 这是必需的,因为 Class 属性在导入时进行评估,那时反向方法将 return 'Reverse Not Found'。 稍后根据需要,在执行时,所有必要的代码片段都将被执行,以提供有效的 URL.
区别:
- reverse() 在函数中使用 & reverse_lazy() 在 class. 中使用
- reverse() 在字符串中使用 & reverse_lazy() 在对象中使用