Created
September 25, 2017 15:08
-
-
Save vitorfs/808ee2cd589ac215b4ff15a364dfc95f to your computer and use it in GitHub Desktop.
cloudflare-like sign up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.contrib.auth.models import User | |
class SignUpForm(forms.ModelForm): | |
username = models.CharField( | |
widget=forms.EmailInput(), | |
label='Email address', | |
required=True, | |
max_length=150 | |
) | |
raw_password = models.CharField(widget=forms.PasswordInput(), label='Password', required=True) | |
class Meta: | |
model = User | |
fields = ('username', 'raw_password') | |
def save(self, commit=True): | |
user = super().save(commit=False) | |
raw_password = self.cleaned_data.get('raw_password') | |
user.set_password(raw_password) | |
if commit: | |
user.save() | |
return user |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth import login | |
from django.shortcuts import render, redirect | |
from .forms import SignUpForm | |
def signup(request): | |
if request.method == 'POST': | |
form = SignUpForm(request.POST) | |
if form.is_valid(): | |
user = form.save() | |
login(request, user) | |
return redirect('home') | |
else: | |
form = SignUpForm() | |
return render(request, 'signup.html', {'form': form}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment