Reverse_lazy和URL加载? [英] Reverse_lazy and URL Loading?

查看:59
本文介绍了Reverse_lazy和URL加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图围绕Django概念进行思考,但是我对URLResolver reverse_lazy()感到困惑.据我所知,当我要反向到尚未加载的URL时,必须使用reverse_lazy().因此,当我创建CBV并声明success_url时,我使用的是reversy_lazy.这意味着在文件执行时不会导入URL.

I'm trying to wrap my head around Django concepts, but I struggle with the URLResolver reverse_lazy(). As far as I know, I have to use reverse_lazy() when I want to reverse to an URL that has not been loaded. So when I create a CBV and state a success_url, I use reversy_lazy. This means that the url is not imported when the file executes.

这使我感到困惑,因为我认为服务器在执行任何操作之前都会在启动时加载所有URL.那么,如何在执行时不加载URL?

This is confusing to me because I think the server loads all the URLs while starting before executing anything. So how come the URL is not loaded in time of execution?

如果有人能给我答案我会很高兴.

I would be very happy if someone would give me an answer to this.

推荐答案

首先,URL解析器本身是惰性的,因此在第一次调用 resolve()或<进行code> reverse()(通常在第一个请求时).但这在这种情况下并不重要.

Well, first of all, the URL resolver itself is lazy, so loading happens when the first call to resolve() or reverse() is made (usually on the first request). But that doesn't matter much in this case.

初始化URL解析器时,它将导入URL配置,而URL配置又将导入视图.因此,在导入视图并设置 success_url 时,解析程序仅在其初始化的一半时完成.此时调用 reverse()将不起作用,因为解析器尚不具备反转视图名称的所有信息.

When the URL resolver is being initialised, it imports your URL configuration, which in turn imports your views. So at the time your view is imported and success_url is set, the resolver is only halfway through its initialisation. Calling reverse() at this point would not work since the resolver doesn't have all the information yet to reverse the view name.

这篇关于Reverse_lazy和URL加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆