我想建立一个网站,通过向他们发送验证邮件来验证用户的身份。为了找到一个好的教程,我来到this 这对我帮助很大;但我仍然有问题。
我不知道为什么没有电子邮件发送到我注册的示例电子邮件,也没有写在PyCharm终端(正如教程中所述)。
你能帮帮我吗,我犯错了哪一部分?我的代码:
#settings.py
SITE_ID = 1
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
"django.template.context_processors.media",
],
},
},
]
# Custom allauth settings
# Use email as the primary identifier
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_REQUIRED = True
# Make email verification mandatory to avoid junk email accounts
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
# Eliminate need to provide username, as it's a very old practice
ACCOUNT_USERNAME_REQUIRED = False
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin and to ensure compatibility with other packages
'django.contrib.auth.backends.ModelBackend',
# 'allauth' specific authentication methods
'allauth.account.auth_backends.AuthenticationBackend',
)
#urls.py
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^accounts/', include('allauth.urls')),
url(r'^$', views.main_page),
url(r'^admin/', admin.site.urls),
url(r'^home/', views.home),
url(r'^wallets/', views.wallets),
url(r'^trans-history/', views.trans_history),
url(r'^reports/', views.reports, name='reports'),
url(r'^general/', views.home, name='general'),
url(r'^register/$', views.register, name='register'),
url(r'^login/$', views.user_login, name='login'),
url(r'^logout/$', views.user_logout, name='logout'),
]
#views.py
def register(request):
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
registration_form = RegistrationForm(data=request.POST)
if user_form.is_valid() and registration_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = registration_form.save(commit=False)
profile.email = user_form.cleaned_data['email']
profile.user = user
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
profile.save()
registered = True
new_user = authenticate(username=user_form.cleaned_data['username'],
password=user_form.cleaned_data['password'], )
login(request, new_user)
else:
print user_form.errors, registration_form.errors
else:
user_form = UserForm()
registration_form = RegistrationForm()
return render(request, 'register.html',
{'user_form': user_form, 'profile_form': registration_form, 'registered': registered})
和我的forms.py:
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'username', 'email', 'password')
password = forms.CharField(required=True, widget=forms.PasswordInput())
confirm_password = forms.CharField(required=True, widget=forms.PasswordInput())
def clean_confirm_password(self):
password = self.cleaned_data.get('password', '')
confirm_password = self.cleaned_data.get("confirm_password", '')
if password != confirm_password:
print("error")
raise forms.ValidationError("Passwords do not match!")
return confirm_password
class RegistrationForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ['user', 'email']