将附加参数传递给 django 的登录 &模板 [英] passing additional arguments to django's login & template
问题描述
我的应用程序使用 django.contrib.auth.views.login 中的 Django 登录视图,以及我制作的自定义模板.
My application uses Django's login view from django.contrib.auth.views.login, with a custom template I made.
我想向我的模板传递一个额外的参数,它将以几种方式更改登录页面.实际上,我正在尝试根据 URL 显示不同的登录页面.
I'd like to pass my template an additional argument which will change the login page in a few ways. Actually, I'm trying to show a different login page based on the URL.
我如何向登录的视图传递额外的参数 &到我的自定义模板?
How can I pass additional arguments to the login's view & to my custom template?
推荐答案
来源显示只有一个地方会影响模板上下文.
The source shows there is only one place that would influence the template context.
def login(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm): # here
因此,您唯一的选择是搭便车 AuthenticationForm
或编写自己的登录视图(顺便说一句,如果您查看代码,这非常).
So your only option is to hitch a ride on AuthenticationForm
or write your own login view (which, by the way, is very simple if you look at the code).
from django.contrib.auth.forms import AuthenticationForm
AuthenticationForm.my_extra_data = 'foobar'
(r'^accounts/login/$', 'django.contrib.auth.views.login', \
{'template_name': 'myapp/login.html', \
'authentication_form': AuthenticationForm }),
模板
{{ form.my_extra_data }}
这篇关于将附加参数传递给 django 的登录 &模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!