[Django]-Django Forbidden HttpResponse in Test-Drive

4👍

Setting is_staff=True allows you to access the admin. It does not let you access all admin pages automatically. You need the ‘add blog post’ permission to be able to access the page to add blog posts.

The easiest option would be to create a superuser, then they would have permissions to access the page.

self.my_admin = User(username='user', is_staff=True, is_superuser=True)
self.my_admin.set_password('password')
self.my_admin.save()

Using the create_superuser password, this simplifies to:

self.my_admin = User.objects.create_superuser('user', email=None, password='password')

Or you could manually add the required permission to the user (or a group that the user belongs to).

from django.contrib.auth.models import User, Permission
self.my_admin = User(username='user', is_staff=True)
permission = Permission.objects.get(content_type__model='blog', codename='add_blog')
self.my_admin.user_permissions.add(permission)

Leave a comment