gpt4 book ai didi

Django:如何更改 AdminTimeWidget 的选择

转载 作者:行者123 更新时间:2023-12-02 14:24:30 25 4
gpt4 key购买 nike

在管理中为 DateTimeField 呈现的 AdminTimeWidget 显示一个时钟图标,当您单击时,您可以选择:“现在午夜 6:00 中午”。

如何将这些选项更改为“16h 17h 18h”?

最佳答案

克里斯有一个很好的答案。作为替代方案,您可以仅使用 javascript 来完成此操作。将以下 javascript 放置在您想要不同时间选项的页面上。

DateTimeShortcuts.overrideTimeOptions = function () {
// Find the first time element
timeElement = django.jQuery("ul.timelist li").eq(0).clone();
originalHref = timeElement.find('a').attr('href');

// remove all existing time elements
django.jQuery("ul.timelist li").remove();

// add new time elements representing those you want
var i=0;
for (i=0;i<=23;i++) {
// use a regular expression to update the link
newHref = originalHref.replace(/Date\([^\)]*\)/g, "Date(1970,1,1," + i + ",0,0,0)");
// update the text for the element
timeElement.find('a').attr('href', newHref).text(i+"h");
// Add the new element into the document
django.jQuery("ul.timelist").append(timeElement.clone());
}
}

addEvent(window, 'load', DateTimeShortcuts.overrideTimeOptions);

关于Django:如何更改 AdminTimeWidget 的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5770973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com