小编Ali*_*ham的帖子

如何在 django DRF 中处理时区而不重复太多?

  • 简介:我的项目TIME_ZONE等于,'UTC'而我有来自太多时区的用户。因此,当我使用 make POSTorPUTdateortimedateTime字段时,我将这些字段转换为UTCbefore serializer.save()。然后,当用户发出GET请求时,我将相同的字段转换回用户的时区,即request.user.timezone
# simplified functions

def localize(usertimzone, date, type):
    """
    type = 'date', 'time', 'datetime'
    """
    from dateutil import parser
    date = parser.parse(date)
    if not date.tzinfo:
        usertimzone = pytz.timezone(usertimzone)
        date = usertimzone.localize(date)
    utc_date = date.astimezone(pytz.utc)
    return utc_date

def normalize(usertimzone, date, type):
    current_user_tz = pytz.timezone(usertimzone)
    date = current_user_tz.localize(date)
    return date
Run Code Online (Sandbox Code Playgroud)
#usages example
    def post(self, request, *args, **kwargs):
        alert_date = …
Run Code Online (Sandbox Code Playgroud)

django timezone pytz django-rest-framework django-timezone

3
推荐指数
1
解决办法
2198
查看次数

稀疏二值分解

如果非负整数 N 的二进制表示不包含两个连续设置为 1 的位,则称为稀疏整数。例如,41 是稀疏的,因为其二进制表示为“101001”并且不包含两个连续的 1。另一方面,26 并不稀疏,因为它的二进制表示是“11010”并且包含两个连续的 1。

\n

如果 P 和 Q 是稀疏的且 N = P + Q,则两个非负整数 P 和 Q 称为整数 N 的稀疏分解。

\n

例如:

\n
8 and 18 are a sparse decomposition of 26 (binary representation of 8 is "1000", binary representation of 18 is "10010");\n9 and 17 are a sparse decomposition of 26 (binary representation of 9 is "1001", binary representation of 17 is "10001");\n2 and 24 are not a sparse decomposition of 26; though …
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
2033
查看次数

如何用 yew 查询和更新 DOM?

有什么办法可以通过 DOM 操作吗use_node_ref?或者,如何document.query_selector()使用红豆杉在 Rust 中进行操作?

use web_sys::HtmlInputElement;
use yew::{
    function_component, functional::*, html,
    NodeRef, Html
};

#[function_component(UseRef)]
pub fn ref_hook() -> Html {
    let input_ref = use_node_ref();
    let all_editables =  input_ref.query_selector("[contenteditable]")
    web_sys::console::(all_editables)

    html! {
        <div>
            <input ref={input_ref} type="number" />
        </div>
    }
}

Run Code Online (Sandbox Code Playgroud)

目标:我有一个富文本编辑器应用程序。我有一个像这样的字符串形式的缩小的 html <h1> this is title</h1><p>hello world</>,我需要获取 DOM 并更改innerHTML以将其设置为此值。

目标2:innerHtml当用户在元素中写入内容时,我还需要更新contenteditable。例如,当用户输入时@john smith,我将创建一个<a>元素,其中包含指向 的个人资料的href链接。John smith

rust reactjs yew web-sys

2
推荐指数
1
解决办法
2357
查看次数