在django中使用电子邮件而不是登录名[英] Using email instead of login name in django

本文是小编为大家收集整理的关于在django中使用电子邮件而不是登录名的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

首先,这不是如何在电子邮件/密码对中进行身份验证的问题,而是如何制作逻辑,如果您喜欢的话,很漂亮的数据结构.

我想在给定的Django项目中使用电子邮件作为用户名.但是,由于至少两个原因,我无法重新使用Auth.user模型提供的字段:

  1. auth.user.username的字段max_length是30个字符,这可能不足以容纳某些电子邮件地址.

  2. auth.user.email不是唯一的 - 对于先决条件,说用户名必须是唯一的.

如此明显的方法是将用户名存储在自定义配置文件中,该简介链接到auth.user.在这种情况下,我们必须处理以下问题:

  1. 为auth.user.username生成唯一的用户名 - 电子邮件的MD5哈希在这里可以很好吗?
  2. 完全删除auth.user.email空的 - 因为它只有75个字符长,而根据RFC 5321(有效电子邮件地址的最大长度是多少?)电子邮件可以长达256个字符.

以下问题源于提议的解决方案:

  1. 一个人将无法重复使用标准操作(例如密码重置等)的内置视图/模板
  2. 如果发送电子邮件更改auth.user.username必须进行更新

为了将油添加到火中,Django开发人员不太可能在任何可预见的将来解决此限制 - 请参见 http://code.djangoproject.com/ticket/11365

所以问题是:还有其他方法可以做吗?您是否在上述解决方案中看到其他缺点?

谢谢!

推荐答案

我有一个自1995年以来一直在商业网站开设的客户(是的,我们在这里谈论早期采用者).无论如何,他们已经拥有一个已建立的用户群,并且名称完全不符合Django的用户名的想法.

我看了几种处理它的方法,他们都觉得自己像骇客(2007年夏天),所以我说拧紧并直接hacked cons.auth.models.user.我只需要更改大约10行代码,增加字段大小并调整验证器.从那时起,我们进行了两次升级 - 0.97 pre => 1.0,1.0 => 1.1.1-每次仅需15分钟即可"端口hack".

这并不漂亮,我可能会因为这样做而在地狱中燃烧,但是这样做的时间比我能弄清楚的其他任何事情要少,而前进端口则是一个完全的非问题.

其他推荐答案

您可能想看看Satchmo如何处理此问题:

.org/chris1610/satchmo/src/tip/satchmo/apps/satchmo_store/encelect/email-auth.py

其他推荐答案

我写了一个关于这个问题的解释: django authettional使用电子邮件地址.它基本上包括:

  • 为电子邮件身份验证创建自定义授权后端.
  • 子类用户创建表格以添加电子邮件地址作为必需字段.
  • 在创建和登录表单中隐藏用户的用户名字段.
  • 在处理创建表单的视图中随机生成一个用户名.
  • 手动向电子邮件列(yuck!)的唯一索引

我的解决方案仍然有2个问题.首先,手动创建数据库索引不好.其次,该电子邮件仍然仅限于75个字符(我没有任何有8,000个用户的系统的问题).但是,它与其他Django和第三方应用程序相当出色.

本文地址:https://www.itbaoku.cn/post/597762.html

问题描述

Firstly, this is not the question how to authenticate on email/password pair, but rather how to produce logical, and if you like, beautiful data structure.

I want to use emails as user names in a given django project. However, I am unable to re-use fields provided by auth.User model for at least two reasons:

  1. auth.User.username 's field max_length is 30 characters, which might not be enough for some email addresses.

  2. auth.User.email is not unique - which is obviously not satisfactory for a prerequisite saying that user names have to be unique.

So an obvious way here is to store username in a custom profile, which is linked to auth.User. In this case we have to deal with following problems:

  1. Generate unique username for auth.User.username - md5 hash of email should be fine here?
  2. Leave out completely auth.User.email empty - since it's only 75 characters long, while according to RFC 5321 (What is the maximum length of a valid email address?) email can be as long as 256 characters.

The following problems stem from the proposed solution:

  1. One is not going to be able to reuse built-in views/templates for standard operations like password reset etc
  2. In case of email change auth.User.username will have to be updated

To add oil into the fire, django developers are not likely to fix this limitation in any foreseeable future - see http://code.djangoproject.com/ticket/11365

So the question is: is there any other way to do it and do you see any other drawbacks in the solution proposed above?

Thanks!

推荐答案

I had a client with a commercial site that had been up since 1995 (yeah, we're talking early adopters here). Anyway, they already had an established user base and the names were totally non-compliant with Django's idea of a username.

I looked at a few ways to handle it and they all felt like hacks (this was Summer of 2007), so I said screw it and hacked contrib.auth.models.User directly. I only had to change about 10 lines of code, increase the field size, and tweak the validator. We've done two upgrades since then -- 0.97-pre => 1.0, and 1.0 => 1.1.1 -- and it's only taken about 15 minutes each time to "port the hack".

It isn't pretty, and I may burn in Hell for doing it like this, but it took less time to do it this way than anything else I could figure out and the forward ports have been a total non-issue.

其他推荐答案

You might want to take a look at how Satchmo handle this problem :

http://bitbucket.org/chris1610/satchmo/src/tip/satchmo/apps/satchmo_store/accounts/email-auth.py

and

http://bitbucket.org/chris1610/satchmo/src/533a63f955f8/satchmo/apps/satchmo_utils/unique_id.py

其他推荐答案

I wrote up an explanation of my solution to this same problem: Django Authentication using an Email Address. It basically consists of:

  • Create a custom authorization backend for email authentication.
  • Subclass the user creation form to add email address as a required field.
  • Hide the username field from the user in the creation and login forms.
  • Randomly generate a username in the view that processes the creation form.
  • Manually ad a unique index to the email column (Yuck!)

My solution has still has 2 problems. First, manually creating a database index is not good. Second, the email is still limited to the 75 characters (I didn't have any issues porting a system with about 8,000 users). But, it plays pretty nicely with the rest of Django and 3rd party apps.