2

我正在尝试制作一个网站,人们只输入他们的电子邮件地址,并且他们使用 cookie 和所有内容登录。在稍后阶段,我会要求他们提供密码和姓名,但不会使用任何用户名。我正在尝试使用 django-registraition 来执行此操作,但是我遇到了错误并且遇到了一些问题。

首先禁用用户名作为登录功能,我把 str(time()) 而不是用户名 - 我一直在寻找每次都会改变的东西。

但是,当我跳过身份验证(我目前不需要)时,我收到错误:

'RegistrationProfile' object has no attribute 'backend'

或者,我可以留下身份验证,但我不知道如何仅使用电子邮件和没有密码进行身份验证。另外,我不知道如何使下一行工作:

auth.login(request, ProfileUser)

如果有人能让我离开这里,那就太棒了。这是一些代码:

我的表格类:

class RegistrationFormCustom(forms.Form):
email = forms.EmailField()
def do_save(self):
    new_u = User(username=str(time()),email= self.cleaned_data.get('email'),)
    new_u.save()
    new_p = Profile.objects.create_profile(new_u)
    new_p.save()
    return new_p

我的观点:

def registerCustom(request, backend, success_url=None, form_class=None,
         disallowed_url='registration_disallowed',
         template_name='registration/registration_form.html',
         extra_context=None,
     initial={}):

form = RegistrationFormCustom(initial=initial)
if request.method == 'POST':
    form = RegistrationFormCustom(initial=initial, data=request.POST)
    if form.is_valid():
        profile = form.do_save()
        profile = auth.authenticate(username = profile.user.email, password = form.cleaned_data.get('pass1'))
        print(profile)
        auth.login(request, profile)
        return redirect('/')

    else:
        pass

return render_jinja(request, 'registration/registration_form.html',
        type="register",
        form = form
        )

我会愉快地发布任何其他需要的剪辑

4

3 回答 3

5

您收到'RegistrationProfile' object has no attribute 'backend'错误是因为用户尚未经过身份验证。要让某人登录,您必须先调用该authenticate方法,这需要密码。所以,你可以做的是:

from django.contrib.auth import load_backend, login, logout
from django.conf import settings

def _login_user(request, user):
    """
    Log in a user without requiring credentials (using ``login`` from
    ``django.contrib.auth``, first finding a matching backend).

    """
    if not hasattr(user, 'backend'):
        for backend in settings.AUTHENTICATION_BACKENDS:
            if user == load_backend(backend).get_user(user.pk):
                user.backend = backend
                break
    if hasattr(user, 'backend'):
        return login(request, user)

然后,要让某人登录,只需_login_user使用请求和User模型调用该函数。(这可能是profile.user你的情况)这样做而不是调用auth.login. 我不确定您将如何确定这是否是有效用户,没有密码或用户名,但我将把它留给您。如果您仍然有问题,请告诉我。

简短说明:

这里基本上发生的是 Django 需要对用户进行身份验证才能通过该login功能登录。该身份验证通常由该authenticate函数完成,该函数需要用户名和密码,并检查提供的密码是否与数据库中的散列版本匹配。如果是这样,它会向User模型添加一个身份验证后端。

因此,由于您没有密码和用户名,您只需要编写自己的方法来将身份验证后端添加到User模型中。这就是我的_login_user) 函数所做的 - 如果用户已经通过身份验证,它只会调用login,否则,它首先将默认后端添加到User模型中,而不检查正确的用户名和密码(就像这样authenticate做)。

于 2011-04-30T18:53:24.450 回答
0

对于阅读此线程的其他人,当我使用 User.objects.create() 而不是 User.objects.create_user() 时,我收到了类似的错误消息。基本上,第一种方法是设置明文密码,而 create_user 加密密码。清除密码将无法进行身份验证。检查您的数据库,如果您设置了明文密码,那么您可能需要使用 create_user() 代替。

作者的请求可以通过简单地使用 create_user() 而不是 user.save() 设置默认用户和密码来解决。

于 2011-09-25T18:43:09.503 回答
0

您可以创建一个已知密码(将其放在 settings.py 中)并像用户输入它一样使用它。使用此创建用户并使用此验证用户。

于 2013-10-11T05:56:20.170 回答