本文介绍了名字,姓氏和电子邮件地址随机填写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当新用户进入注册页面时,有时候用户首先看到

我们已经在我们的网络应用程序中实现了django-allauth上一次登录的用户的姓名,姓氏和电子邮件地址填写在注册表单中。这真的是随机的,有时候。这也发生在个人资料编辑表单中,这只是简单的django表单,采用CBV(FormView)中的self.request.user的用户实例,如下所示:

 $ {

$ b kwargs = super(ProfileView,self).get_form_kwargs()
kwargs.update({
'instance':self.request.user
})
返回kwargs

我们正在使用allauth的基本默认设置网站安装说明。我们现在只用于电子邮件注册和登录。



allauth settings.py(所有其他设置,我们都有相同的指示,如安装的应用程序,中间件等):

 #DJANGO-ALLAUTH 
ACCOUNT_ADAPTER ='users.adapter.AccountAdapter'
LOGIN_URL ='/ accounts / login /'
LOGIN_REDIRECT_URL ='预订:booking_add'
ACCOUNT_FORMS = {'signup':'users.forms.SignupForm',}
ACCOUNT_AUTHENTICATION_METHOD ='email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION ='none'
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_LOGOUT_ON_GET = True
SOCIALACCOUNT_EMAIL_VERIFICATION ='无'
ACCOUNT_EMAIL_SUBJECT_PREFIX =
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True

AUTH_USER_MODEL ='users.User'

AUTHENTICATION_BACKENDS =(
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',

as signupView 我们使用默认值,但我们也尝试添加never_cache装饰器帮助):

  class SignupView(AllauthSignupView):
template_name ='account / signup.html'

signup = never_cache(SignupView.as_view())

注册格式,覆盖默认值:

  class SignupForm(AllauthSignupForm):
设置中定义的django-allauth用法在ACCOUNT_FORMS
title = forms.CharField(label = _('Title'),widget = forms.Select(choices = choices.USER_TITLE))
first_name = forms.CharField(label = _ ('First Name'))
last_name = forms.CharField(label = _('Last Name'))
email = forms.EmailField(widget = forms.TextInput(attrs = {'type' 'email',}))
password1 = SetPasswordField(label = _(Password))
password2 = CustomPasswordField(label = _(Password(again)))

def __init __(self,* args,** kwargs):

super(SignupForm,self).__ init __(* args,** kwargs)
set_form_field_order(self,[title ,first_name,last_name,email,password1,password2])
for self.fields中的字段:
self.fields [field] .widget.attrs [' class'] ='mdl-textfield__input'

class Meta:
fields =('title','first_name','last_name','email','password1','password2' )

我们使用默认的allauth LoginForm和LoginView。



它是Django 1.8.7,nginx(1进程),gunicorn(4名工作者)通过主管(1个进程)运行。

解决方案

当我们在Django CBV FormView中有这样的事情时,我们发现在另一个表单上造成这样的麻烦:

  def get_initial (self):
user = self.request.user
如果有的话:
self.initial.up date({
'title':user.title,
'first_name':user.first_name,
'last_name':user.last_name,
'email':user.email ,
'phone':user.phone,
'street':user.street,
'city':user.city,
'zip_code':user.zip_code,
'country':user.country
})
return self.initial

我们已经解决了如下:

  def get_initial(self):
user = self.request。 user
initial = super(PassengerAddStep1FormView,self).get_initial()
如果有的话:
initial.update({
'title':user.title,
' first_name':user.first_name,
'last_name':user.last_name,
'email':user.email,
'phone':user.phone,
'street' :user.stree t
'city':user.city,
'zip_code':user.zip_code,
'country':user.country
})
return initial


we have implemented django-allauth into our web app and we are facing random leaks.

When a new user enters signup page, sometimes user sees first name, last name and email address of lastly logged user prefilled in signup form. This occurs really randomly, just sometimes. This also happens in profile edit form, which is just simple django form taking instance of user from self.request.user in CBV (FormView) like this:

def get_form_kwargs(self):
    kwargs = super(ProfileView, self).get_form_kwargs()
    kwargs.update({
        'instance': self.request.user
    })
    return kwargs

We are using basic default setup of allauth from the website installation instructions. We use it as for now just for email registration and login.

allauth settings.py (all other settings we have the same like in instructions eg. installed apps, middlewares etc):

# DJANGO-ALLAUTH
ACCOUNT_ADAPTER = 'users.adapter.AccountAdapter'
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = 'bookings:booking_add'
ACCOUNT_FORMS = {'signup': 'users.forms.SignupForm', }
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_LOGOUT_ON_GET = True
SOCIALACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_EMAIL_SUBJECT_PREFIX = ""
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True

AUTH_USER_MODEL = 'users.User'

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

as signupView we use default one, but we also tried adding never_cache decorator (did not help):

class SignupView(AllauthSignupView):
    template_name = 'account/signup.html'

signup = never_cache(SignupView.as_view())

SignupForm, overriding default one:

class SignupForm(AllauthSignupForm):
    """ django-allauth usage defined in settings in ACCOUNT_FORMS"""
    title = forms.CharField(label=_('Title'), widget=forms.Select(choices=choices.USER_TITLE))
    first_name = forms.CharField(label=_('First Name'))
    last_name = forms.CharField(label=_('Last Name'))
    email = forms.EmailField(widget=forms.TextInput(attrs={'type': 'email',}))
    password1 = SetPasswordField(label=_("Password"))
    password2 = CustomPasswordField(label=_("Password (again)"))

    def __init__(self, *args, **kwargs):

        super(SignupForm, self).__init__(*args, **kwargs)
        set_form_field_order(self, ["title", "first_name", "last_name", "email", "password1", "password2"])
        for field in self.fields:
            self.fields[field].widget.attrs['class'] = 'mdl-textfield__input'

    class Meta:
        fields = ('title', 'first_name', 'last_name', 'email', 'password1', 'password2')

We use default allauth LoginForm and LoginView.

It's Django 1.8.7, nginx (1 process), gunicorn (4 workers) running through supervisor (as 1 process).

解决方案

We have found troubles causing this on another form when we had something like this in Django CBV FormView:

def get_initial(self):
    user = self.request.user
    if something:
        self.initial.update({
            'title': user.title,
            'first_name': user.first_name,
            'last_name': user.last_name,
            'email': user.email,
            'phone': user.phone,
            'street': user.street,
            'city': user.city,
            'zip_code': user.zip_code,
            'country': user.country
        })
    return self.initial

we have fixed this as follows:

def get_initial(self):
    user = self.request.user
    initial = super(PassengerAddStep1FormView, self).get_initial()
    if something:
        initial.update({
            'title': user.title,
            'first_name': user.first_name,
            'last_name': user.last_name,
            'email': user.email,
            'phone': user.phone,
            'street': user.street,
            'city': user.city,
            'zip_code': user.zip_code,
            'country': user.country
        })
    return initial

这篇关于名字,姓氏和电子邮件地址随机填写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:31