Django User Register

phansivang

Phansivang

Posted on April 11, 2022

Django User Register

register.html

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

Enter fullscreen mode Exit fullscreen mode

forms.py

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
Enter fullscreen mode Exit fullscreen mode

views.py

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})
Enter fullscreen mode Exit fullscreen mode

urls.py

from django.urls import path
from . import views

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

Enter fullscreen mode Exit fullscreen mode

Enjoy !!

πŸ’– πŸ’ͺ πŸ™… 🚩
phansivang
Phansivang

Posted on April 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related