Register Django allauth without password

Is it possible to create user accounts with an email address as a username and no other data such as a password using allauth? The goal is to make the registration process as simple as possible. Is it possible to skip the password pass during registration and update when confirming email ?. I tried this script in a python shell ( ./manage.py shell ) and had successful exits.

 In [1]: from django.contrib.auth.models import User In [2]: User.objects.create(username='nopass') Out[2]: <User: nopass> In [3]: User.objects.all() Out[3]: [<User: userone>, <User: nopass>] In [4]: usr=User.objects.all()[1] In [5]: usr.set_password('pwdnotset') In [6]: usr.save() In [7]: from django.contrib.auth import authenticate In [8]: authenticate(username='nopass',password='pwdnotset') Out[8]: <User: nopass> 

I referred to this link and found that at that time there were no such settings for allauth. However, a response was sent in 2013. It would be useful if a method was created to create a user without a password when registering with some allauth configuration. Thanks in advance.

+7
source share
3 answers

I do not see a way to do this in allauth yet, except by setting up the code for your project.

Unlike other answers, the code in allauth.account.forms inherits SignUpForm from your user registration form:

 # in allauth.account.forms.py class BaseSignupForm(_base_signup_form_class()): 

BaseSignupForm is used both for "standard" registration, and for registration in a social account. In the "standard" registration, it is subclassed as SignUpForm and adds password fields:

 # in allauth.account.forms.py class SignupForm(BaseSignupForm): def __init__(self, *args, **kwargs): super(SignupForm, self).__init__(*args, **kwargs) self.fields['password1'] = PasswordField(label=_("Password")) if app_settings.SIGNUP_PASSWORD_ENTER_TWICE: self.fields['password2'] = PasswordField( label=_("Password (again)")) 

So, I tried to hide the field in the view template, but no luck. Perhaps using FormHelper in crispyforms is a way.

+1
source

One option is to create a custom registration view and override the registration template to publish to a custom view. In this view, you can use your own RegistrationForm, which does not require a password.

The form:

 class CustomUserCreationForm(forms.ModelForm): class Meta: model = CustomUser fields = ('email',) def save(self, commit=True): instance = super().save(commit=False) instance.set_unusable_password() if commit: instance.save() return instance 

View:

 class MySignupView(SignupView): form_class = CustomUserCreationForm 

Address:

 path('accounts/signup/custom/', MySignupView.as_view(), name="account_signup_custom"), 

signup.html:

 <form method="post" action="{% url 'account_signup_custom' %}"> 
+1
source

I think creating a signup form with an overridden signup() which sets an unusable password to user and saying that allauth to use this form for the registration process will solve this problem. Create the registration form by overriding signup () as shown below.

 class UserCreationForm(forms.ModelForm): username = forms.CharField(label=_("username")) # declare other fields also ... def signup(self, request, user): user.username = self.cleaned_data['username'] # make sure you are saving all needed data for user model. user.set_unusable_password() user.save() 

in settings.py, tell allauth to use this form to register

 ACCOUNT_SIGNUP_FORM_CLASS = 'yourapp.forms.UserCreationForm' 

The following is the ipdb stack trace for the above context.

  48 import ipdb;ipdb.set_trace(); ---> 49 user.email = self.cleaned_data['email'] 50 user.username = self.cleaned_data['username'] ipdb> user <User: test_user> ipdb> user.save() *** IntegrityError: NOT NULL constraint failed: user.password ipdb> user.set_unusable_password() ipdb> user.save() ipdb> user <User: test_user> ipdb> 
0
source

Source: https://habr.com/ru/post/1243674/


All Articles