i was trying to use sending email modual from django in order to send sign up email for each user when sign up im facing this error :UNIQUE constraint failed: auth_user.username
` i was trying to use sending email modual from django in order to send sign up email for each user when sign up im facing this error :UNIQUE constraint failed: auth_user.username`
# this is my views.py
from django.shortcuts import render ,redirect
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from . import forms
# Create your views here.
def home(request):
return render(request,'home.html')
@login_required
def customer_page(request):
return render(request,'home.html')
@login_required
def courier_page(request):
return render(request,'home.html')
def sign_up(request):
form = forms.SignUPForm()
if request.method=='POST':
form=forms.SignUPForm(request.POST)
if form.is_valid():
email=form.cleaned_data.get('email').lower()
user= form.save(commit=False)
user.useranme = email
user.save()
login(request,user,backend='django.contrib.auth.backends.ModelBackend')
return redirect('/')
return render(request,'sign_up.html',{
'form':form
})
` now i have created signals.py to use email forms `
# my signals.py file
from email.base64mime import body_decode
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.mail import send_mail
from django.conf import settings
from django.contrib.auth.models import User
from django.template.loader import render_to_string
from django.core import mail
connection = mail.get_connection()
@receiver(post_save,sender=User)
def send_welcome_email(sender,instance,created,**kwarg):
if created and instance.email:
connection.open()
body=render_to_string(
'welcome_email_template.html',
{
'name':instance.get_full_name()
}
)
email2 = mail.EmailMessage(
'Welcome to fast Parcle',
body,
settings.DEFAULT_FROM_EMAIL,
[instance.email],
fail_silently=False,
)
connection.send_messages([email2])
connection.close()
#also my settings.py
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap4',
'social_django',
'core.apps.CoreConfig',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'fastparcel.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
]
WSGI_APPLICATION = 'fastparcel.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
LOGIN_URL='/sign_in/'
LOGIN_REDRICTED_URL='/'
AUTHENTICATION_BACKENDS = (
'social_core.backends.facebook.FacebookOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_FACEBOOK_KEY="######"
SOCIAL_AUTH_FACEBOOK_SECRET="############"
SOCIAL_AUTH_FACEBOOK_SCOPE=['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS= {
'fields':'id,name,email'
}
SECURE_SSL_REDIRECT = False
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST='smtp.gmail.com'
EMAIL_USE_TLS=True
EMAIL_PORT=587
EMAIL_HOST_USER='####'
EMAIL_HOST_PASSWORD='####'
DEFAULT_FROM_EMAIL='FAST Parcle <[email protected]>'
ACCOUNT_SIGNUP_FORM_CLASS = 'core.forms.SignupForm' ```
CodePudding user response:
Someone is signing up with a username that already exists. The username field in the auth_user table has a uniqueness constraint that prevents you from inserting a row that has a username that already exists in the table.
CodePudding user response:
thanks @Eshter the solution was as mention above :create an "application password" for this specific program, which you would find out if you went to the link they gave you in the error message and have to use this created password inside the code exactly the way i used to use a regular password