Django退出按钮

Joh*_*ash 2 django django-forms django-admin django-views django-login

这似乎是一个愚蠢的问题,但我找不到任何帮助.如何在每个视图上创建一个注销按钮,如管理页面中的那个?

小智 9

使用模板继承:https: //docs.djangoproject.com/en/dev/topics/templates/#template-inheritance 或include tag:https: //docs.djangoproject.com/en/dev/ref/templates/builtins/ ?从= olddocs#包括

模板继承的示例:我们的应用程序上的所有页面都有一个基本模板:

# base.html #
<html>
<head>...</head>
<body>
    <a href="/logout">logout</a>  # or use the "url" tag: {% url logout_named_view %}

    {% block content %} {% endblock %}
</body>
</html>


# other_pages.html #

{% extends "base.html" %}
{% block content %}
    <div class="content">....</div>
    ....
    ....
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

现在,我们在从base.html继承的所有页面上都有一个注销链接

包含标记的示例:

# user_panel.html #
<div class="user_panel">
    <a href="/logout">logout</a>
</div>

# other_pages #
<html>
<head>...</head>
<body>
    {% include "user_panel.html" %}
    ...
    ...
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我建议使用模板继承来解决您的问题