如何在 Django 登录表单中预设用户名?
在对现有解决方案不满意之后,我为 Django 编写了一个 OpenId 提供程序。
如果现在有人想在某个地方验证自己的身份 http://tejp.de/users/abc/< /a> 并且需要登录,我想显示用户名预设为“abc”的登录表单。 像 redirect_to_login
这样的标准函数似乎没有为此提供任何参数,而且我也不知道如何在重定向到 django.contrib.auth 中的登录视图时预设该值手动.views.login
。 此外,似乎没有简单的方法可以将附加参数值传递到模板,以便我可以在那里插入预设值。
有没有办法在登录表单中自动填写用户名? 如果可能的话,我想使用普通的登录视图,而不是复制并粘贴所有登录代码并编辑必要的更改。
After being dissatisfied with the existing solutions I wrote an OpenId provider for Django.
If now somebody wants to authenticate himself somewhere as http://tejp.de/users/abc/ and needs to login for that, I want to display the login form with the username preset to "abc". The standard functions like redirect_to_login
don't seem to provide any parameters for this and I also don't see how I could preset that value when redirecting to the login view in django.contrib.auth.views.login
manually. Also there seems to be no easy way to get an additional parameter value through to the template, so that I could maybe insert the preset value there.
Is there a way to automatically fill in a username in the login form? If possible I'd like to use the normal login view for this, not copy&paste all the login code and edit in the necessary changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,django 完全是 python,源代码是你的朋友。
这是
redirect_to_login
的原始来源。看起来您甚至可以使用带有适当参数的 URL 链接来覆盖 login_url 。 像
您不需要新表单,您只需要预先填充现有的登录表单。
Remember, django is all python, and the source is your friend.
Here's the original source for
redirect_to_login
.It looks like you could even just override login_url with a link to a URL that takes the appropriate parameters. Something like
You don't need a new form, you just need to prepopulate the existing login form.
解决上述问题的一些方法是:
编写您自己的身份验证表单并在显示之前预填写用户名。
使用 javascript 从 url 中删除用户名并将其放入表单中。
使用
Some of ways to solve the above problem are:
Write your own authentication form and prefill the username before displaying.
Use javascript to strip a username from url and put it down in the form.