New registration and login are the basic functions when creating a website. After completing the new registration, I wanted to omit the work of entering the necessary items on the login screen again, so I investigated the method.
The project structure is as follows. The setting directory is config, and the application directory is created in app and accounts.
.
├── accounts
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── config
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── app
│ ├── __init__.py
│ ├── apps.py
│ ├── forms.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── static
│ └── css
│ └── style.css
└── templates
├── base.html
├── registration
│ ├── base.html
│ ├── logged_out.html
│ ├── login.html
│ └── signup.html
└── app
└── index.html
The settings directory, the URLconf in django.contrib.auth specified in it, and the URlconf in the application directory are shown below.
config/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app.urls')),
path('accounts/', include('django.contrib.auth.urls')), #Urls provided by Django in advance.To py
path('accounts/', include('accounts.urls')), #Urls I created.To py
]
django/contrib/auth/urls.py
from django.contrib.auth import views
from django.urls import path
urlpatterns = [
path('login/', views.LoginView.as_view(), name='login'),
path('logout/', views.LogoutView.as_view(), name='logout'),
path('password_change/', views.PasswordChangeView.as_view(), name='password_change'),
path('password_change/done/', views.PasswordChangeDoneView.as_view(), name='password_change_done'),
path('password_reset/', views.PasswordResetView.as_view(), name='password_reset'),
path('password_reset/done/', views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/', views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]
app/urls.py
from django.urls import path
from . import views
app_name = 'app'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
]
accounts/urls.py
from django.urls import path
from . import views
app_name = 'accounts'
urlpatterns = [
path('signup/', views.SignUpView.as_view(), name='signup'),
]
First, check the state before mounting.
accounts/views.py
from django.urls import reverse_lazy
from django.views import generic
from .forms import UserCreateForm
class SignUpView(generic.CreateView):
form_class = UserCreateForm
template_name = 'registration/signup.html'
success_url = reverse_lazy('login')
form_class
.template_name ='registration / signup.html'
.success_url = reverse_lazy ('login')
. In this case, it's'login', so when new registration is complete, you'll be taken to the login page (registration / login.html) that Django has prepared in advance.template_name ='registration / signup.html'
. (* The html of the login page is prepared in advance, but the new registration screen is not prepared, so you need to create it yourself.)After the new registration is completed, you only need to make a small change to acccouts / views.py to complete the login without going through the login screen.
accounts/views.py
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth import login, authenticate #add to
from .forms import UserCreateForm
class SignUpView(generic.CreateView):
form_class = UserCreateForm
success_url = reverse_lazy('app:index') #Change
template_name = 'registration/signup.html'
#Added below
def form_valid(self, form):
response = super().form_valid(form)
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(self.request, user)
return response
There are two tasks to do: specifying success_url
and overriding the form_valid method.
success_url = reverse_lazy ('app: index')
to change the screen that transitions after registration is completed from'login'to'app: index', that is, the top page.response = super (). form_valid (form)
.and substitute the value entered in the newly registered userneme field for username.
form.cleaned_data` indicates the data that passed the input validation.password = form.cleaned_data.get ('password1')
. to authenticate a user and password. ʻAuthenticate ()
takes a username and password as arguments and returns a User object if the password is valid for the username. If it is invalid, None is returned.login (self.request, user)
. login ()
takes an HttpRequest object and a User object as arguments.Recommended Posts