且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在模式窗口中从Django登录

更新时间:2023-12-03 15:34:22

如果要在所有页面中显示该表单,则需要添加表单(***将其命名为 login_form ,这样就不会与每个View的上下文冲突)。

If you want to display the form in all pages, you need to add the form (better call it login_form so it doesn't conflict with other forms you may have) to every View's context.

为避免在重复执行此操作在所有视图中,Django都有上下文处理器。将它们包含在 settings.py TEMPLATES 变量中。示例

To avoid doing that repeatedly in all Views, Django has Context Processors. You include them in settings.py's TEMPLATES variable. Example

TEMPLATES = [{
    ...
    'OPTIONS': {
        'context_processors': [
            ...
            'myapp.context_processors.add_my_login_form',
        ],
    }]

然后,创建一个名为 context_processors.py 的文件,并添加 add_my_login_form()函数。该函数将 login_form 返回到所有请求的请求上下文。

Then, create a file called context_processors.py and add the add_my_login_form() function in there. The function returns the login_form to the request context of all requests.

def add_my_login_form(request):
    return {
        'login_form': LoginForm(),
    }

由于要在每个页面中呈现表单,因此***使用模板缓存。

Since you are rendering the form in every page, it maybe good to use template caching.