django 1.3 url 模板标签和基于类的视图
我刚刚开始将一个应用程序从 1.1 迁移到 1.3。
我开始深入了解基于阶级的观点,并感到震惊,但并不是很好。
我有一些抱怨,但这里的具体问题是:
这是我可以将 url 模板标记与通用的基于类的视图一起使用的唯一方法吗?
带有参数的 Django 反向 URL 到基于类的视图
即必须命名每个 URL 条目?
这对我来说似乎很荒谬,因为 Django 的基本哲学之一是 DRY,但我们在这里...... RY-ing.....
谢谢进步。
编辑:
所以我有 https://gist.github.com/1877374
并收到错误 模板语法错误 渲染时捕获 NoReverseMatch:未找到参数“()”和关键字参数“{}”的“views.HomeView.as_view”的反向。
我使用这个方法不正确吗?
切线:
我想更多地解释一下为什么我相信我们正在 RY-ing,如果我们必须命名 urls.py 文件中的每个条目,
我的 urls.py 通常看起来像这样 https://gist.github.com/1877462
我完全了解解耦。
这里的要点是,我们有能力在需要时这样做。当我需要时,我绝对会使用名称功能。否则,为什么我要花时间和精力向每个条目重复添加 url 并命名每个条目,而它们通常与views.py 中的类/函数的名称相同?
也许这应该分成一个关于SO的单独问题。
I'm just starting to migrate an app I have to 1.3 from 1.1.
I'm starting to get in the thick of class based views and am blown away, but not really in a good way.
I have some gripes but the specific question here is:
Is this the only way i can use the url template tag with a generic class-based view?
Django reverse url with parameters to a class based view
i.e. having to name every single url entry?
It seems ridiculous to me as one of the fundamental philosophies of Django is DRY and yet here we are.... RY-ing.....
Thanks in advance.
Edit:
So I have https://gist.github.com/1877374
and get the error
TemplateSyntaxError
Caught NoReverseMatch while rendering: Reverse for 'views.HomeView.as_view' with arguments '()' and keyword arguments '{}' not found.
Am I using this incorrectly?
Tangent:
I'd like to explain a little bit more about why I believe we are RY-ing if we have to name every single entry in the urls.py file
my urls.py typically looks like
https://gist.github.com/1877462
I understand completely about decoupling.
The point here is that we have the ability to do so when required. I absolutely use the name feature, when i need to. Otherwise, why would i want to spend the time and energy to redundently add url to every entry and name every entry when often they will be the same as the name of the class/funciton in views.py?
Maybe this should be branched into a seperate question on SO.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有一个相当夸张的解释,解释了为什么你不能在不命名基于类的视图的情况下反转它们。我不太熟悉 Django 内部结构,所以很高兴得到纠正。
使用基于函数的视图,
您可以反转
my_app.views.my_view
,因为它是可调用视图函数的路径。对于基于类的视图,
您无法反转
my_app.views.MyView
,因为它不是可调用的视图对象。可调用视图是MyView.as_view()
。如果您将MyView.as_view()
分配给视图中的变量,如下所示:请注意
, `my_view`),url()
基本上只是一个函数,可以更轻松地添加命名 url 模式。如果您确实不想命名您的网址,您可以编写自己的函数来自动为您生成名称。那么您将能够反转
my_view
而无需命名它。此选项与命名您的网址一样重复,所以我认为您不会喜欢它!但是,当您将
MyView.as_view()
直接放入 URL 模式时,它是一个匿名函数。它尚未分配给任何变量,因此没有可用于反转它的路径。同样,您将无法反转以下内容:请注意
url()
基本上只是一个函数,可以更轻松地添加命名 url 模式。如果您确实不想命名您的网址,您可以编写自己的函数来自动为您生成名称。Here's a rather hand-waving explanation of why you can't reverse the class based views without naming them. I'm not really familiar with the Django internals, so I'm happy to be corrected.
With a function based view,
you can reverse
my_app.views.my_view
, because it is the path of a callable view function.With a class based view,
you can't reverse
my_app.views.MyView
, because it isn't a callable view object. The callable view isMyView.as_view()
. If you assignedMyView.as_view()
to a variable in your views as follows:Note that
, `my_view`),url()
is basically just a function that makes it easier to add named url patterns. If you really don't want to name your urls, you could write your own function that automatically generates names for you.then you would be able to reverse
my_view
without naming it. This option is just as much repeating as naming your url, so I don't think you'll like it!.However when you put
MyView.as_view()
directly in your url pattern, it's an anonymous function. It hasn't been assigned to any variable, so there is no path you can use to reverse it. Similarly, you wouldn't be able to reverse the following:Note that
url()
is basically just a function that makes it easier to add named url patterns. If you really don't want to name your urls, you could write your own function that automatically generates names for you.首先,这不是重复自己。你在哪里命名 URL 两次?那会重复你自己。
其次,URL 模式的命名不是必需的 - 但提供了许多优点 - 这就是推荐它的原因。它还为您提供了更改视图方法名称的灵活性,而无需更改模板。您可以决定一组 url 名称,并将它们交给您的设计人员来处理模板,并且您可以按照自己喜欢的方式自由命名视图方法(或类)。
第三,您需要将完整路径传递给视图方法 - 因此对于基于类的视图,它需要是
as_view
,并确保您传递正确数量和类型的参数;并且不要混合位置参数和关键字参数。或者,您可以通过命名 URL 模式来避免上述大部分情况。
First, this is not repeating yourself. Where are you naming the URL twice? That would be repeating yourself.
Second, naming of url patterns is not required - but offers many advantages - which is why it is recommended. It also provides you the flexibility of changing your view method names without having to change your templates. You can decide on a set of url names and hand them off to your designer to work on the templates, and you are free to name your view methods (or classes) the way you like.
Third, you need to pass the full path to the view method - so it needs to be
as_view
for class-based views and make sure you pass the correct number and type of arguments; and don't mix positional and keyword arguments.Or, you can avoid most of the above by naming your URL patterns.
我不明白这如何违反了 DRY 原则 - 它们都是独立的视图,执行不同的操作,并且每个视图都被赋予唯一的标识符,以免在反转时发生冲突。如果有的话,使用命名 URL 将减少您必须在模板级别编写的代码,并使您的 url 方案更具可读性
I don't see how this is violating the DRY principle - they are all separate views that do different things and they are each being given a unique identifier so as not to collide when being reversed. If anything, using named URLs will reduce the code you have to write at the template level and make your url scheme far more readable