django admin permissions code example
Example 1: loginrequiredmixin
from django.contrib.auth.mixins import LoginRequiredMixin
LOGIN_URL = 'your_url'
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()
Example 4: staff user is not restricting permission in django
class UserAdmin(BaseUserAdmin):
...
def has_add_permission(self, request, obj=None):
return request.user.is_superuser
def has_delete_permission(self, request, obj=None):
return request.user.is_superuser