django模板可以找到它的命名url,还是基本模板可以知道哪个模板正在扩展它?(Can a django template find out its named url or can a base template know which template is extending it?)
在我的URLConf中,我的URL被整齐地命名,所以我可以进行反向查找并保持一切干燥。
我的所有模板都扩展了一个名为
base.html
的通用模板。 在base.html
,在很多HTML框架中,我显示了一个登录表单。我还有一个特定的视图,它使用Django提供的
auth_views.login
视图,它显示我的自定义模板login.html
。 与所有其他模板一样,此模板扩展了base.html模板。在这种情况下,我希望
base.html
模板不会显示其登录表单(login.html
模板已经显示了登录表单)。 为了做到这一点,base.html
要么知道它被用于扩展login.html
,要么知道导致base.html被扩展的命名URL的名称是'myapp-login'
。你能建议一种方法吗? 我正在考虑编写自己的视图,该视图将使用包含
suppress_header_login_form
var的额外上下文调用auth_views.login
。 基于此var,base.html
模板可以禁止登录表单。 但是,我试图看看是否有更好的方法来实现这一目标。In my URLConf, I have my URLs neatly named so I can do reverse lookups and keep everything DRY.
All my templates extend a universal template named
base.html
. Inbase.html
, amongst a lot of HTML framework, I display a login form.I also have a specific view which uses Django's provided
auth_views.login
view, which displays my custom templatelogin.html
. This template, like all others, extend the base.html template.In cases like this, I would like that the
base.html
template would not display its login form (thelogin.html
template already is displaying a login form). In order to do this,base.html
either needs to know that it is being used to extendlogin.html
, or, know that the named URL which resulted in base.html being extended is has the name'myapp-login'
.Can you suggest a way to do this? I am thinking of writing my own view which will call
auth_views.login
with extra context which includes asuppress_header_login_form
var. Based on this var, thebase.html
template could suppress the login form. However, I am trying to see if there is a nicer way to accomplish this.
原文:https://stackoverflow.com/questions/7928664
最满意答案
它的
length
为0
因为数组length
属性返回比最高数字索引高的数字 。如果你这样做,你会得到1的
length
:var a = []; a[0] = a; a.length; // 1
您可以将字符串属性的属性赋值给一个数组,并且它将“工作”,因为数组是对象并且可以具有额外的属性,但只有数字(整数)索引属性参与像
.length
或.slice()
这样的数组行为。 如果你想使用字符串键,你应该使用一个普通的对象:var a = {};
(Plain)对象没有
length
(除非你自己创建属性),但是你可以使用Object.keys(a).length
来获得属性的数量(如果你不关心IE <9,它没有实现.keys()
- 虽然有一个polyfill )。It has a
length
of0
because the Arraylength
property returns a number one higher than the highest numeric index.You'd get a
length
of1
if you did this:var a = []; a[0] = a; a.length; // 1
You can assign properties with string keys to an array and it will "work" in that arrays are objects and can have extra properties, but only the numerically (integer) indexed properties participate in array behaviour like
.length
or.slice()
. If you want to use string keys you should be using a plain object:var a = {};
(Plain) Objects don't have a
length
(unless you create the property yourself), but you can useObject.keys(a).length
to get the number of properties (if you don't care about IE < 9, which doesn't implement.keys()
- though there is a polyfill).
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
因为你在执行mysql_query()时会得到一个mysql资源。 使用类似mysql_fetch_assoc()东西来获得下一行。 它返回一个列名称为索引的数组。 你的情况可能是COUNT(*) 。 这里有一个修复和你的代码片段的一些小改进: $rt = mysql_query("SELECT COUNT(*) FROM persons") or die(mysql_error()); $row = mysql_fetch_row($rt); if($row) echo "
Number:< ...
-
如何获取对象长度(How to get object length [duplicate])[2022-03-26]
对于支持Object.keys()的浏览器,您可以简单地做: Object.keys(a).length; 否则(特别是在IE <9)中,您可以使用for (x in y)循环自己循环遍历对象: var count = 0; var i; for (i in a) { if (a.hasOwnProperty(i)) { count++; } } hasOwnProperty是确保您只是从对象字面值计算属性,而不是从其原型“继承”的属性。 For browsers su ... -
由于数组衰减为指针,因此无法在函数中执行长度计算:函数获取指针而不是数组。 您需要按照函数在打印3时的方式执行计算,或者使用宏来计算数组长度: #define GET_LENGTH(inp) (sizeof(inp)/sizeof(*inp)) 这是链接到ideone的演示。 Since arrays decay to pointers, you cannot perform length calculation in a function: the function gets a pointer, no ...
-
我们可以从一个方法返回一个空数组而不是null,这被称为空对象设计模式。 考虑下面的代码 Person[] res = find(name); for(String e : res) { System.out.println(e); } 如果find()没有找到任何人,它将返回一个空数组。 如果查找返回null,那么代码将需要将其视为特殊情况。 我们应该记住,空数组是不可变的,所以使用单例而不是每次创建它都是合乎逻辑的 private static final Pe ...
-
它的length为0因为数组length属性返回比最高数字索引高的数字 。 如果你这样做,你会得到1的length : var a = []; a[0] = a; a.length; // 1 您可以将字符串属性的属性赋值给一个数组,并且它将“工作”,因为数组是对象并且可以具有额外的属性,但只有数字(整数)索引属性参与像.length或.slice()这样的数组行为。 如果你想使用字符串键,你应该使用一个普通的对象: var a = {}; (Plain)对象没有length (除非你自己创建属性),但 ...
-
使用Object.keys(obj).length来获取Object的长度 var tiles = { tile1 : { header : "header1", desc : "desc1" }, tile2 : { header : "header2", desc : "desc2" } }; console.log(Object.key ...
-
$#array表示法为您提供了@array最后一个元素的索引。 @ARGV包含程序的命令行参数。 如果您在运行程序时未提供任何参数,则该列表将为空。 所以它进入了while ,其中$sub是0 ,大于-1 。 所以它永远不会进入循环。 因此,该程序只应输出一行空[] 。 $ perl foo.pl [] $ 有关@ARGV和perldata for $#array更多详细信息,请参阅perlvar 。 The $#array notation gives you the index of the last ...
-
您可以使用变量进行计数。 var filter = userInput.value.toUpperCase(), count = 0; for (var i = 0; i < myArrayElements.length; i++) { if (myArrayElements[i].toUpperCase().indexOf(filter) != -1) { // ^^^^^^^^^^^^^^^ only if that is really a string!!! ...