我目前正在尝试根据它们出现的次数来注释和计算一些日期。
visits = Subs.objects.filter(camp=campdata, timestamp__lte=datetime.datetime.today(), timestamp__gt=datetime.datetime.today()-datetime.timedelta(days=30)).\
values('timestamp').annotate(count=Count('timestamp'))
Run Code Online (Sandbox Code Playgroud)
如果我在 for 循环中打印这个,
for a in visits:
print(a)
Run Code Online (Sandbox Code Playgroud)
我会在 Json 中取回以下内容。
{'timestamp': datetime.datetime(2018, 10, 5, 15, 16, 25, 130966, tzinfo=<UTC>), 'count': 1}
{'timestamp': datetime.datetime(2018, 10, 5, 15, 16, 45, 639464, tzinfo=<UTC>), 'count': 1}
{'timestamp': datetime.datetime(2018, 10, 6, 8, 43, 24, 721050, tzinfo=<UTC>), 'count': 1}
{'timestamp': datetime.datetime(2018, 10, 7, 4, 54, 59, tzinfo=<UTC>), 'count': 1}
Run Code Online (Sandbox Code Playgroud)
这是一个正确的方向,但是,它正在计数到第二个......我只需要几天,这样发生在 2018、10、5 的事件就会被计数:例如 2。
谁能引导我走向正确的方向?
此外,将日期转换为对 json/api 更友好的最“django”方式是什么?
我理想的 json 返回类似于
{'timestamp': 2018-10-5, 'count': 2}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有以下使用 react-mapbox-gl 包装器的简单 React 组件。到目前为止,它很棒,但是我在尝试使用 useState 钩子更新状态时发现了一个小烦恼。
function Location(props) {
const [selectedMarker, setSelectedMarker] = useState(null);
const [markerBlock, setMarkerBlock] = useState(false)
const {payload} = useContext(MarkerContext);
const [latlong, setLatlong] = useState(null);
console.log(payload);
const Map = ReactMapboxGl({
accessToken:
'tokenHiddenforstackoverflow',
});
const getLatlng = (map, event) => {
setLatlong(event.lngLat);
}
return (
<>
<Map
style="mapbox://styles/mapbox/satellite-streets-v9"
containerStyle={{
height: '100vh',
width: '100%'
}}
onClick={getLatlng}
center={[18.432723671572944, -33.926210020024826]}
>
<ZoomControl />
</Map>
</>
);
}
export default Location;
Run Code Online (Sandbox Code Playgroud)
每当我更新 LatLong 状态时,地图框背景都会变黑并且整个地图都会重新加载。不确定是编码/结构错误还是来自 Mapbox 的错误。我也尝试将其重组为更小的组件,但没有区别。
有任何想法吗?
我有一个数组,比如说
[
{
lesson: 1,
title: "Welcome",
slug: "welcome",
active: active,
breakDay: false,
started_time: new Date(),
finished_time: null,
completed: true,
sublesson: [
{
lesson: 1.1,
title: "Evaluation",
slug: 'evaluation',
completed: false,
answers: []
}
],
answers: []
},
{
lesson: 2,
title: "Example",
slug: "example",
active: active,
breakDay: false,
started_time: null,
finished_time: null,
completed: false,
sublesson: [
{
lesson: 2.1,
title: "example2,
slug: 'example2,
answers: []
}
],
answers: []
}
]
Run Code Online (Sandbox Code Playgroud)
我需要找到一种方法来制定第一个项目以达到特定条件。
我在我的 React 应用程序中尝试过,但很快意识到它实际上是在检查第一个项目而不是整个数组的这些条件是否为真。
{
lessons !== null …Run Code Online (Sandbox Code Playgroud) 我有一个链接列表。
我在点击时添加了一个称为“处于活动状态”的类。同时,我想删除所有现有的“处于活动状态”,但单击的链接除外。类别中只有一个元素是“活动的”,因为它会“活跃”页面。(使用bulma CSS)
这是到目前为止我尝试过的代码。它在添加类,但不删除它们。
class Menu extends Component {
constructor(props) {
super(props);
this.state = {addClass: false}
};
handleClick(e) {
if(e.target.class === 'is-active'){
e.target.className = '';
console.log('remove')
}else{
e.target.className = 'is-active';
console.log('add class')
}
}
render() {
<ul className="menu-list">
{ this.props.getList.map(list =>
<Link onClick={this.handleClick.bind(this)} key={list.id} to="">{list.title}</Link>
)}
</ul>
}
}
export default SideMenu;
Run Code Online (Sandbox Code Playgroud)
咨询将不胜感激。