没有密码的Django注册设置 [英] Django-registration setup without password

查看:108
本文介绍了没有密码的Django注册设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个网站,人们只会放置他们的电子邮件地址,并使用cookies和所有人登录。在稍后的阶段,我会要求他们提供密码和名称,但不会使用用户名。我试图用django-registraition这样做,但是我收到错误,我有一些问题。



首先禁用用户名作为登录功能,我把str(time())替换为用户名 - 我正在寻找会每次更改的内容。 >

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

 'RegistrationProfile'对象没有属性'后端'

或者,我可以离开认证,然后我不知道如何验证它只有电子邮件和没有密码。另外,我不知道如何使下一行工作:

  auth.login(request,ProfileUser)

如果有人可以让我离开这里,这将是非常棒的。以下是一些代码:



我的表单类:

  class RegistrationFormCustom (forms.Form):
email = forms.EmailField()
def do_save(self):
new_u =用户(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)
如果request.method =='POST':
form = RegistrationFormCustom(initial = initial ,data = request.POST)
如果form.is_valid():
profile = form.do_save()
profile = auth.authenticate(userna me = 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

我会发布任何其他快速的剪切需求


解决方案

你得到'RegistrationProfile'对象没有属性'后端'错误,因为用户尚未验证。要登录某人,您必须首先调用验证方法,这需要密码。所以,你可以做的是这样的:

  from django.contrib.auth import load_backend,login,logout 
从django.conf导入设置

def _login_user(请求,用户):

登录用户而不需要凭据(使用``login``从
``django.contrib.auth``,首先找到一个匹配的后端)


如果不是hasattr(user,'backend'):
对于后端在设置.AUTHENTICATION_BACKENDS:
如果用户== load_backend(后端).get_user(user.pk):
user.backend =后端
break
如果hasattr(用户, '后端')
返回登录(请求,用户)

然后,在请求和 User 模型中调用 _login_user 函数。 (在这种情况下,这将是 profile.user )可以这样做,而不是调用 auth.login 。我不知道如何确定这是否是有效的用户,没有密码或用户名,但我会把它留给你。



简短说明



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



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


I am trying to make a website, where people only put their email addresses and they are logged in with cookies and all. At a later stage, i will ask them provide password and names, but NO username will be used. I am trying to do this with django-registraition, but i get errors and i have a few problems.

First to disable usernames as a login feature, i put str(time()) instead of username - i was looking for something that will change every time.

However, when I skip the authentication (which i currently don't need) i get error:

'RegistrationProfile' object has no attribute 'backend'

Alternatively, i can leave the authentication but then i don't know how to authenticate it only with email and no password. Also, i don't know how to make the next line work:

auth.login(request, ProfileUser)

If anyone can get me out of here, it would be awesome. Here is some code:

my form Class:

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

my view:

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
        )

and i will post any other snipped required happily

解决方案

You're getting the 'RegistrationProfile' object has no attribute 'backend' error because the user is not yet authenticated. To log someone in, you have to call the authenticate method first, which requires a password. So, what you can do instead, is this:

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)

Then, to log someone in, just call the _login_user function with the request and User model. (This will be profile.user in your case, probably) Do this instead of calling auth.login. I'm not sure on how you're going to determine whether this is a valid user or not, without a password or username, but I'll leave that to you. If you still have trouble, let me know.

Short Explanation:

What basically happens here is that Django requires a user to be authenticated in order to be logged in via the login function. That authentication is usually done by the authenticate function, which requires a username and password, and checks whether the supplied password matches the hashed version in the database. If it does, it adds an authentication backend to the User model.

So, since you don't have a password and username, you just have to write your own method for adding the authentication backend to the User model. And that's what my _login_user) function does - if the user is already authenticated, it just calls login, otherwise, it first adds the default backend to the User model, without checking for a correct username and password (like authenticate does).

这篇关于没有密码的Django注册设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆