还有更好的地方来放置我们的负载标签吗?

问题描述 投票:0回答:1
django django-templates
1个回答
0
投票

在 Django 中,{% load %} 标签通常放置在文件的顶部,以提高可读性。这可以清楚地表明模板中使用了哪些库。以下是重构 HTML 的方法:

{% load socialaccount %} <!-- Loading at the top -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {% block title %} <title>Sign In</title> {% endblock %}
</head>
<body>
    {% block content %}
        <h1>Sign in</h1>
        <a href="{% provider_login_url 'google'%}">Login with google</a>
    {% endblock %}
</body>
</html>

这样,任何阅读代码的人都会立即知道此模板中正在使用 socialaccount 库。这是一个很小的改变,但它可以在可读性上产生很大的变化!

© www.soinside.com 2019 - 2024. All rights reserved.