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'
1from django.contrib.auth.decorators import permission_required
2
3@permission_required('auth.view_user')
4def users_list_view(request):
5 pass
6
1# all your import goes here for Models.py
2...
3
4# Adding Permissions to a Model.
5class BlogPost(models.Model):
6 ... # model fields
7 class Meta:
8 permissions = [('can_write_blog', 'Can Write Blog')]
9
10
11# Checking permissions in Views.py
12if request.user.has_perm('app_name.can_write_blog'):
13 # give access to blog post form
14else:
15 # restrict user from access the page.
16
1def my_view(request):
2 # Individual permissions
3 permissions = Permission.objects.filter(user=request.user)
4
5 # Permissions that the user has via a group
6 group_permissions = Permission.objects.filter(group__user=request.user)
7