I recently created a Django template using allauth for an Abstract User.
I followed William Vincent’s best practices in his book and the settings according to the documentation but I’m currently stuck.
I’d like to support multiple sign up forms to create a user. The 2 sign up forms I’d like to support are:
- Account creation, which uses the typical convention of:
# my_project/setings.py
AUTH_USER_MODEL = 'users.CustomUser'
LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'
ACCOUNT_SESSION_REMEMBER = True
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username'
ACCOUNT_USER_MODEL_EMAIL_FIELD = "email"
- A User Subscription Form (with simply email and no password), which uses what I came up with:
# users/forms.py
class CustomUserSubscribeForm(forms.ModelForm):
class Meta:
model = CustomUser
fields = ('email', )
email = forms.EmailField(
label=_(''),
widget=forms.TextInput(
attrs={
'placeholder': _('john@email.com')
}
)
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_show_labels = False
self.helper.form_method = 'POST'
self.helper.form_class = 'form-inline justify-content-center'
self.helper.layout = Layout(
Div(
FieldWithButtons('email', Submit('Submit', 'submit', css_class='btn btn-outline primary')),
Submit('submit', u'Submit', css_class='btn btn-success'),
css_class='form-inline'
)
)
# users/views.py
class SubscribePageView(CreateView):
form_class = CustomUserSubscribeForm
success_url = reverse_lazy('subscribe')
template_name = 'subscribe.html'
# users/urls.py
from .views import SubscribePageView
urlpatterns = [
path('subscribe', SubscribePageView.as_view(), name='subscribe'),
]
These views/html pages render but the issue I’m facing is when I try to use the Subscription form, I receive an integrity error:
duplicate key value violates unique constraint "users_customuser_username_80452fdf_uniq"
DETAIL: Key (username)=() already exists.
Any help/guidance would be much appreciated here as I’ve been trying to handle this a million different ways for the past week. Thank you!