django user specific access code example
Example 1: django login required decorator
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
...
Example 2: loginrequiredmixin
from django.contrib.auth.mixins import LoginRequiredMixin
LOGIN_URL = 'your_url'
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