1# Django User Model Fields
2username
3first_name
4last_name
5email
6password
7groups
8user_permissions
9is_staff
10is_active
11is_superuser
12last_login
13date_joined
1def my_view(request):
2 if not request.user.is_authenticated:
3 return render(request, 'myapp/login_error.html')
1from django.contrib.auth import authenticate, login
2
3def my_view(request):
4 username = request.POST['username']
5 password = request.POST['password']
6 user = authenticate(request, username=username, password=password)
7 if user is not None:
8 login(request, user)
9 # Redirect to a success page.
10 ...
11 else:
12 # Return an 'invalid login' error message.
13 ...
14
1from django.contrib.auth.mixins import LoginRequiredMixin
2
3class MyView(LoginRequiredMixin, View):
4 login_url = '/login/'
5 redirect_field_name = 'redirect_to'
6
1myuser.groups.set([group_list])
2myuser.groups.add(group, group, ...)
3myuser.groups.remove(group, group, ...)
4myuser.groups.clear()
5myuser.user_permissions.set([permission_list])
6myuser.user_permissions.add(permission, permission, ...)
7myuser.user_permissions.remove(permission, permission, ...)
8myuser.user_permissions.clear()
9