This is my memorandum. I did a lot of research for self-study, but since there were many explanations of version 2 etc., it was complicated, so I will summarize it to some extent. I am writing it in the hope that it will be of benefit to those who have encountered similar problems. Please watch with warm eyes! (Please do not hesitate to make any corrections)
You can understand most things by doing a tutorial (appropriate) https://docs.djangoproject.com/ja/3.1/intro/tutorial01/
Usage environment
・ OS ... windows10
・ Python ... 3.7.6
・ Django ... 3.1.3
I'm sorry if there is an oversight. The package is probably
pip install django
pip install pillow
It should have been just ...! !!
I will write mainly where I stumbled from here.
I wanted to implement the function, so I made my own login function. As a flow
2:
model.py
class User(AbstractUser):
pass
And just inherited AbstractUser. For the time being, the official document says that you should make your own by overriding. You can add your favorite columns by editing here. [Refer to Abstract User] https://docs.djangoproject.com/en/3.1/topics/auth/customizing/
3: I was thinking of the following options as a method of implementing the login and logout functions.
view.py
from django.contrib.auth import authenticate, login
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
...
else:
# Return an 'invalid login' error message.
...
as well as
Method using View Reference source: https://docs.djangoproject.com/ja/3.1/topics/auth/default/
Since the amount of code is reduced when using View, I implemented it here, but it seems that the method that is intuitively easy to understand is the method of processing within the former method. When using View, be sure to check the variables inside. The causes of useless code and errors are listed relatively. Speaking this time, Form is the default because it is used when giving a class name in html. I edit only template_name and inherit the rest as it is. Logout is implemented in the same way, so it is omitted.
4: The new registration screen stumbled the most. I will write from the conclusion.
view.py
def signup(request):
if request.method == 'POST':
form = UserCreateForm(request.POST)
if form.is_valid():
form.save()
else:
print(form.errors)
return redirect('/login')
return render(request, 'loginauth/create_user.html')
def redirect_view(request):
response = redirect('/login')
return response
forms.py
class UserCreateForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['username'].widget.attrs['class'] = 'form-control'
self.fields['password1'].widget.attrs['class'] = 'form-control'
self.fields['password2'].widget.attrs['class'] = 'form-control'
self.fields['email'].widget.attrs['class'] = 'form-control'
class Meta:
model = User
fields = ("username", "password1","password2", "email",)
It's like that. I will supplement it.
The most complicated part personally Views.py ``` form = UserCreateForm (request.POST)` `` was. To briefly summarize what you are doing There is a Form called UserCreationForm for creating a User, Set Model: User and fields on the form. Extract the contents of views.py that match fields from request.POST and check if password1 and 2 match with form.is_valid (). If they match, save as is.
It is a flow. Reference source: https://docs.djangoproject.com/ja/3.1/topics/auth/default/ https://docs.djangoproject.com/ja/3.1/topics/auth/default/#django.contrib.auth.forms.UserCreationForm If you read around here, you can see how to write it.
Recommended Posts