ユーザー登録

6263 ワード

レジスタ.HTML
<form method="POST">
    {% csrf_token %}
    {{ form|crispy }}<br>
    <button type="submit">Sign Up</button>
    <a href="{% url 'login' %}">Login</a>
    </form>

フォーム.パイ
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import User

class registerForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['username', 'password1', 'password2', 'email']

    def __init__(self, *args, **kwargs): (This function is for remove help text at Django native register Form)
        super(registerForm, self).__init__(*args, **kwargs)

        for fieldname in ['username', 'password1', 'password2', 'email']:
            self.fields[fieldname].help_text = None
ビューパイ
from django.shortcuts import render, redirect
from .forms import registerForm
def registerPage(request):
    if request.method == 'POST':
        form = registerForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/login')
    else:
        form = registerForm()
    return render(request, 'app/register.html', {'form': form})
URL.パイ
from django.urls import path
from . import views

urlpatterns = [
    path('register/',views.registerPage,name='register'),
]

エンジョイ!