[PYTHON] [Django] Field names, user registration, and login methods that can be used in the User model

Introduction

This article is a study memo for beginners who are also aiming to break away from Django beginners. There is a possibility of mistakes in word selection and interpretation.

To touch

--What is the User model? --How to make user --Implementation of login / logout

What is the User model?

By default, Django includes a database for user management, which allows you to authenticate and manage users without hassle.

The User model has the following inheritance relationship and has the field names shown below. Usermodel.png

You can also access the data like any other model object.

from django.contrib.auth.model import User

#By the way, the get method returns an error if the value cannot be obtained.
User.objects.get(username='admin') 
User.objects.get(pk=1)

#By the way, the all method returns a queryset object
#Can be used like a list
User.objects.all() 

How to make a User

--Make on the console

(virtualenv)~~~~~$python manage.py createsuperuser

Create a superuser with all privileges to access the Admin site.

--Make on admin site admin.JPG After running server, go to the Admin site and click Add

--Create in views.py

from django.contrib.auth.models import User

User.objects.create_user(username,email,password)
#If you want to jump to the page you logged in to immediately after registering as a user
#user = User.objects.create_user(username,email,password)
#Is convenient

Login / logout

from django.contrib.auth import authenticate, login, logout

def loginfunc(request):
      #If it matches the User model, it returns an object, otherwise it returns none, so as follows
      #Use if statement
      #Immediately after registering User
      #user = User.objects.create_user(username,email,password)Can be substituted with
      user = authenticate(request, username=username, password=password)
        if user is not None:
            #Login
            login(request, user)
            return redirect('userpage')
        else:
            return redirect('signup')

def logoutfunc(request):
    #Log out
    logout(request)
    return redirect('signup')


Recommended Posts

[Django] Field names, user registration, and login methods that can be used in the User model
Goroutine (parallel control) that can be used in the field
Goroutine that can be used in the field (errgroup.Group edition)
A timer (ticker) that can be used in the field (can be used anywhere)
Functions that can be used in for statements
Easy program installer and automatic program updater that can be used in any language
QPS control that can be used in the field (Rate Limit) Limits execution to n times per second
Basic algorithms that can be used in competition pros
[Django] About users that can be used on template
Summary of statistical data analysis methods using Python that can be used in business
Simple statistics that can be used to analyze the effect of measures on EC sites and codes that can be used in jupyter notebook
Steps to change table and column names in your Django model at the same time
Scripts that can be used when using bottle in Python
How to set variables that can be used throughout the Django app-useful for templates, etc.-
I made it because I want JSON data that can be used freely in demos and prototypes
I bought and analyzed the year-end jumbo lottery with Python that can be executed in Colaboratory
About the matter that torch summary can be really used when building a model with Pytorch
33 strings that should not be used as variable names in python
Python standard input summary that can be used in competition pro
Python standard module that can be used on the command line
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
About the matter that the re.compiled object can be used for the re.match pattern
Easy padding of data that can be used in natural language processing
Hide the warning that zsh can be used by default on Mac
Implement a Custom User Model in Django
A collection of Numpy, Pandas Tips that are often used in the field
Python scikit-learn A collection of predictive model tips often used in the field
[2015.02.22] Youtube-dl has been updated and can no longer be used in previous versions.
A personal memo of Pandas related operations that can be used in practice
I made a familiar function that can be used in statistics with Python
Python scikit-learn A collection of predictive model tips often used in the field
File types that can be used with Go
Methods can be defined outside the class definition
Pass login user information to view in Django
[Django] css in the project cannot be read
The problem that the ifconfig command cannot be used
Overview and useful features of scikit-learn that can also be used for deep learning
plotly trace and layout templates that are likely to be used in scatter plots
How to filter foreign keys that can be selected on the Django admin screen
Geographic information visualization of R and Python that can be expressed in Power BI
Set up an FTP server that can be created and destroyed immediately (in Python)
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
Morphological analysis and tfidf (with test code) that can be done in about 1 minute
In Python3.8 and later, the inverse mod can be calculated with the built-in function pow.
The story that sendmail that can be executed in the terminal did not work with cron