首页 \ 问答 \ django模板可以找到它的命名url,还是基本模板可以知道哪个模板正在扩展它?(Can a django template find out its named url or can a base template know which template is extending it?)

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. In base.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 template login.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 (the login.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 extend login.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 a suppress_header_login_form var. Based on this var, the base.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
更新时间:2022-09-08 16:09

最满意答案

它的length0因为数组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 of 0 because the Array length property returns a number one higher than the highest numeric index.

You'd get a length of 1 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 use Object.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).

相关问答

更多
  • 因为你在执行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:< ...

  • 对于支持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!!! ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。