如何在 Django 登录表单中预设用户名?

发布于 2024-07-25 15:08:30 字数 534 浏览 3 评论 0原文

在对现有解决方案不满意之后,我为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-08-01 15:08:30

请记住,django 完全是 python,源代码是你的朋友。

这是redirect_to_login的原始来源。

def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    "Redirects the user to the login page, passing the given 'next' page"
    if not login_url:
        login_url = settings.LOGIN_URL 
    return HttpResponseRedirect('%s?%s=%s' % (login_url, urlquote(redirect_field_name), urlquote(next)))

看起来您甚至可以使用带有适当参数的 URL 链接来覆盖 login_url 。 像

return redirect_to_login(login_url=reverse('my_login_view', [], {'username': username}))

您不需要新表单,您只需要预先填充现有的登录表单。

Remember, django is all python, and the source is your friend.

Here's the original source for redirect_to_login.

def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    "Redirects the user to the login page, passing the given 'next' page"
    if not login_url:
        login_url = settings.LOGIN_URL 
    return HttpResponseRedirect('%s?%s=%s' % (login_url, urlquote(redirect_field_name), urlquote(next)))

It looks like you could even just override login_url with a link to a URL that takes the appropriate parameters. Something like

return redirect_to_login(login_url=reverse('my_login_view', [], {'username': username}))

You don't need a new form, you just need to prepopulate the existing login form.

俏︾媚 2024-08-01 15:08:30

解决上述问题的一些方法是:

  1. 编写您自己的身份验证表单并在显示之前预填写用户名。

  2. 使用 javascript 从 url 中删除用户名并将其放入表单中。

    使用

Some of ways to solve the above problem are:

  1. Write your own authentication form and prefill the username before displaying.

  2. Use javascript to strip a username from url and put it down in the form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文