I am learning Python (3) and Django (1.7) after a while in PHP (not POO). I am rebuilding my own website that has public members. My members have a user account, a public profile and a private profile. When a member signs in, an email is sent to the admins to say that a new member has joined the team and a new email is sent to the new member to say hello.
I am quite new with Django and did all my saves and email sending in the view. I have the idea that it's not the best practice. Can you help me improve my code?
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
slug = models.SlugField(max_length=30)
avatar = models.ImageField(null=True, blank=True, upload_to="images/")
def __str__(self):
return self.user
class UserProfilePublic(models.Model):
FREQUENCY_CHOICES = (
('', ''),
('1', 'Moins de 2 romans'),
('2', 'Entre 2 et 5 romans'),
('3', 'Entre 6 et 10 romans'),
('4', 'Plus de 2 romans')
)
user = models.OneToOneField(User)
website = models.URLField(blank=True)
facebook = models.URLField(blank=True)
twitter = models.URLField(blank=True)
google = models.URLField(blank=True)
hobbies = models.CharField(max_length=250, blank=True)
books_frequency = models.CharField(max_length=1, choices=FREQUENCY_CHOICES, default='')
favorite_types_of_books = models.TextField(blank=True)
favorite_books = models.TextField(blank=True)
quote = models.CharField(max_length=250, blank=True)
def __str__(self):
return self.user
class UserProfilePrivate(models.Model):
user = models.OneToOneField(User)
address = models.CharField(max_length=250, blank=True)
address_bis = models.CharField(max_length=250, blank=True)
zipcode = models.CharField(max_length=5, blank=True)
address_bis = models.CharField(max_length=30, blank=True)
def __str__(self):
return self.user
forms.py
class RegisterForm(forms.Form):
username = forms.CharField(min_length=5, max_length=30, required=True)
email = forms.EmailField(max_length=75, required=True)
password = forms.CharField(widget=forms.PasswordInput, required=True)
password_check = forms.CharField(widget=forms.PasswordInput, required=True)
def clean(self):
cleaned_data = super(RegisterForm, self).clean()
username = cleaned_data.get('username')
email = cleaned_data.get('email')
password = cleaned_data.get('password')
password_check = cleaned_data.get('password_check')
recaptcha = cleaned_data.get('g-recaptcha-response')
# Username
if User.objects.filter(username=username).exists():
msg = "Ce nom d'utilisateur n'est pas disponible"
self.add_error('username', msg)
# Email
if User.objects.filter(email=email).exists():
msg = "Cet email est déjà associé à un utilisateur"
self.add_error('email', msg)
# Password
if password_check != password:
msg = "Le mot de passe et sa confirmation doivent être identiques"
self.add_error('password_check', msg)
return cleaned_data
views.py
def register(request):
if request.user.is_authenticated():
return redirect('/')
else:
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username'].strip().capitalize()
email = form.cleaned_data['email']
password = form.cleaned_data['password']
slug = username.replace("'","-")
slug = slugify(slug)
#Database save
user = User.objects.create_user(username, email, password)
profile = UserProfile(user= user, slug= slug)
profile.save()
profile_public = UserProfilePublic(user= user)
profile_public.save()
profile_private = UserProfilePrivate(user= user)
profile_private.save()
#Email sent to admins
email_from = ''
email_to = ['', '']
email_subject = ""
email_message = ""
send_mail(subject=email_subject, message=email_message, from_email=email_from, recipient_list=email_to, fail_silently=False)
#Email sent to the new member
email_from = ''
email_to = [email]
email_subject = ""
email_message = ""
send_mail(subject=email_subject, message=email_message, from_email=email_from, recipient_list=email_to, fail_silently=False)
#Success message
messages.success(request, "Votre inscription est désormais terminée. <a href=''>Connectez-vous</a> pour démarrer l'aventure.")
else:
form = RegisterForm()
return render(request, 'membres/register.html', locals())