传递额外的参数来Django的登录和放大器;模板 [英] passing additional arguments to django's login & template

查看:143
本文介绍了传递额外的参数来Django的登录和放大器;模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用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 乘车或写自己的登录视图(其中,顺便说一句,是的非常的简单如果你看一下code)。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆