Django:如何从模板中识别主叫视图? [英] Django: How can I identify the calling view from a template?

查看:144
本文介绍了Django:如何从模板中识别主叫视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本

有没有一种简单的内置方式来识别Django模板中的调用视图,而不需要额外的上下文变量?

Is there a simple, built-in way to identify the calling view in a Django template, without passing extra context variables?

长(原始)版本:

我的一个Django应用程序有几个不同的视图,每个视图都有自己的命名URL模式,它们都渲染相同的模板。有一个很少的模板代码需要根据被调用的视图而变化,太小,不值得为每个视图设置单独的模板的开销,所以我最好需要找到一种方法来识别模板中的调用视图

One of my Django apps has several different views, each with its own named URL pattern, that all render the same template. There's a very small amount of template code that needs to change depending on the called view, too small to be worth the overhead of setting up separate templates for each view, so ideally I need to find a way to identify the calling view in the template.

我已经尝试将视图设置为传递额外的上下文变量(例如view_name)来标识调用视图,而且我也尝试使用 {%ifequal request.path/ some / path /%} 比较,但这些解决方案似乎都非常优雅。有没有更好的方法从模板中识别主叫?有没有办法访问视图的名称或URL模式的名称?

I've tried setting up the views to pass in extra context variables (e.g. "view_name") to identify the calling view, and I've also tried using {% ifequal request.path "/some/path/" %} comparisons, but neither of these solutions seems particularly elegant. Is there a better way to identify the calling view from the template? Is there a way to access to the view's name, or the name of the URL pattern?

更新1 :关于这个简单的一个例子,我误解了MVC,我理解MVC,但是 Django不是一个真正的MVC框架。我相信我的应用程序的设置方式与Django对MVC的使用是一致的:这些视图描述了数据的呈现,模板描述了数据的呈现方式。只是发生了我有许多准备不同数据的视图,但都使用相同的模板,因为数据以相同的方式呈现给所有的视图。

Update 1: Regarding the comment that this is simply a case of me misunderstanding MVC, I understand MVC, but Django's not really an MVC framework. I believe the way my app is set up is consistent with Django's take on MVC: the views describe which data is presented, and the templates describe how the data is presented. It just happens that I have a number of views that prepare different data, but that all use the same template because the data is presented the same way for all the views. I'm just looking for a simple way to identify the calling view from the template, if this exists.

更新2:感谢所有人答案。我认为这个问题正在被过度考虑 - 正如我原来的问题所提到的,我已经考虑并尝试了所有的建议的解决方案 - 所以我把它简化为一个简短版本,现在是问题的顶部。现在看来,如果有人简单地发布否,那将是最正确的答案。)

Update 2: Thanks for all the answers. I think the question is being overthought -- as mentioned in my original question, I've already considered and tried all of the suggested solutions -- so I've distilled it down to a "short version" now at the top of the question. And right now it seems that if someone were to simply post "No", it'd be the most correct answer :)

更新3:卡尔·迈尔(Carl Meyer)发表了不,再次感谢大家。

Update 3: Carl Meyer posted "No" :) Thanks again, everyone.

推荐答案

不,这将是一个坏主意。直接从模板引用视图函数名称会导致视图层和模板层之间的过紧紧耦合。

No, and it would be a bad idea. To directly refer to a view function name from the template introduces overly tight coupling between the view layer and the template layer.

这里有一个更好的解决方案是Django的模板继承系统。定义一个公共的父模板,在每个视图的版本中需要更改(小)区域的块。然后定义每个视图的模板从父级开始,并适当地定义该块。

A much better solution here is Django's template inheritance system. Define a common parent template, with a block for the (small) area that needs to change in each view's version. Then define each view's template to extend from the parent and define that block appropriately.

这篇关于Django:如何从模板中识别主叫视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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