1>>> from django.contrib.auth.models import User
2>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
3
4# At this point, user is a User object that has already been saved
5# to the database. You can continue to change its attributes
6# if you want to change other fields.
7>>> user.last_name = 'Lennon'
8>>> user.save()
9
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
3LOGIN_URL = 'your_url'
1def sample_view(request):
2 current_user = request.user
3 print current_user.id
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