Задать вопрос

Тел: +7 965 3737 888

462

Просмотров

3

Ответов

Django Registration without username

A simple adaptation of RegistrationFormUniqueEmail from django-registration http://code.google.com/p/django-registration to allow users to register without using a username. This works great with the code from this comment: http://www.djangosnippets.org/snippets/74/#c195 to allow users to completely eliminate the need for a username.

Вопрос полезен? Да0/Нет0
file_2481.py(815.0б)
None

Ответы (3):

Ответdsaracini:13.06.2012
Ответ полезен? Да0/Нет0

The solution above will only work the first time a duplicate is found. For example, if joe@yahoo.com registers, his username will be "joe". If joe@gmail.com tries to register, it will find "joe" and will get a count of 1 and username will be "joe2". However, if joe@hotmail.com tries to register, the local part will be "joe". It will still get a count of 1 and it will try to set username to joe2 and your application will get an error. This is because filter is "username=joe" (thus, not finding "joe2"). A better solution, and one that will prevent this error is:

c = User.objects.filter(username__startswith=localpart).count()

Ответgwrtheyrn:12.09.2011
Ответ полезен? Да0/Нет0

Actually the code in my last comment was buggy. Working version:

if not self.errors:
    localpart = self.cleaned_data['email'].split('@',1)[0][:25]
    c = User.objects.filter(username=localpart).count()
    if c > 0:
        localpart += str(c + 1)
    self.cleaned_data['username'] = localpart

Ответgwrtheyrn:06.09.2011
Ответ полезен? Да0/Нет0

I updated the username generation a little... Didn't really like the solution with the total user count.

if not self.errors:
    localpart = self.cleaned_data['email'].split('@',1)[0][:25]
    c = User.objects.filter(username=localpart).count()
    if c > 1:
        localpart += c
    self.cleaned_data['username'] = localpart