forms.py
from django import forms from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User from django.db import models class RegistrationForm(UserCreationForm): email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'E-mail'})) class Meta: model = User fields = ['username'] def clean_email(self): email = self.cleaned_data['email'] try: User.objects.get(email=email) except User.DoesNotExist: return email raise forms.ValidationError('email already exist') def save(self, commit=True): user = super(RegistrationForm, self).save(commit=False) user.username = self.cleaned_data['email'] user.email = self.cleaned_data['email'] if commit: user.is_active = False user.save() return user
views.py
from django.shortcuts import render, HttpResponseRedirect from django.contrib import auth from .forms import RegistrationForm def register(request): if request.method == "POST": form = RegistrationForm(request.POST) if form.is_valid(): user = form.save() # user.set_password() user = auth.authenticate(username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password1')) auth.login(request, user) return HttpResponseRedirect('/profile') else: form = RegistrationForm() return render(request, 'fighter/register.html', {'form': form})
Python 3.5 and Django1.10