user authentication django code example
Example 1: django login
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)
...
else:
...
Example 2: django user permission check
...
class BlogPost(models.Model):
...
class Meta:
permissions = [('can_write_blog', 'Can Write Blog')]
if request.user.has_perm('app_name.can_write_blog'):
else:
Example 3: how to create staff account in django
user = User.objects.create_user('john', '[email protected]', 'johnpassword')
user.is_staff=True
user.save()