OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to show permissions in django admin project?

  • Thread starter Thread starter mightycode Newton
  • Start date Start date
M

mightycode Newton

Guest
I have a django app. And I try in the admin panel of django restrict user permissions with the AUTHENTICATION AND AUTHORIZATION tab.

But I noticed that something is missing in my existing django app on the AUTHENTICATION AND AUTHORIZATION tab.

Because I installed a clean install from django. And the AUTHENTICATION AND AUTHORIZATION tab looked like:

enter image description here

and the AUTHENTICATION AND AUTHORIZATION tab in my existing django app looks like:

enter image description here

So as you can see in my existing django app the right part is missing of the user permissions.

What I have done? Is checking the versions. And they are the same(clean version and exsting version)

I am using django version: 5.0.6. and the libraries

I checked the libraries installed with pip freeze of my exisiting version:

Code:
anyio==4.2.0
asgiref==3.7.2
attrs==23.1.0
azure-common==1.1.28
azure-core==1.29.6
azure-storage-blob==12.19.0
azure-storage-common==2.1.0
certifi==2023.7.22
cffi==1.15.1
charset-normalizer==3.2.0
click==8.1.7
colorama==0.4.6
coreapi==2.3.3
coreschema==0.0.4
cryptography==39.0.0
defusedxml==0.7.1
Django==5.0.6
django-allauth==0.52.0
django-cors-headers==3.10.1
django-dotenv==1.4.2
django-filter==23.2
django-storages==1.14.2
djangorestframework==3.14.0
drf-spectacular==0.26.4
drf-yasg==1.20.0
exceptiongroup==1.2.0
gunicorn==21.2.0
h11==0.14.0
idna==3.4
inflection==0.5.1
isodate==0.6.1
itypes==1.2.0
Jinja2==3.1.2
jsonschema==4.19.0
jsonschema-specifications==2023.7.1
Markdown==3.4.4
MarkupSafe==2.1.3
oauthlib==3.2.2
packaging==23.1
Pillow==10.0.0
psycopg2==2.9.7
pycparser==2.21
PyJWT==2.6.0
pyrsistent==0.19.3
python-dateutil==2.8.2
python3-openid==3.2.0
pytz==2023.3.post1
PyYAML==6.0.1
referencing==0.30.2
requests==2.31.0
requests-oauthlib==1.3.1
rpds-py==0.10.2
ruamel.yaml==0.17.32
ruamel.yaml.clib==0.2.7
six==1.16.0
sniffio==1.3.0
sqlparse==0.4.4
typing_extensions==4.7.1
tzdata==2023.3
uritemplate==4.1.1
urllib3==2.0.4
uvicorn==0.23.2
waitress==2.1.2

And part of settings.py file of the existing app:

Code:
LOGOUT_REDIRECT_URL = "/"
ACCOUNT_LOGOUT_REDIRECT_URL = "/"


REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ],
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',

    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),

    'SWAGGER_SETTINGS': {
        'LOGIN_URL': 'rest_framework:login',
        'LOGOUT_URL': 'rest_framework:logout'
    }


}


# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'CET'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/


STATIC_URL = '/static/'
MEDIA_URL = '/media/'

STATIC_DIRS = [
    (BASE_DIR, 'static')
]
STATICFILES_DIRS = [BASE_DIR / 'static']


STATIC_ROOT = BASE_DIR / 'staticfiles/'
MEDIA_ROOT = BASE_DIR / 'media'

admin.py looks:

Code:
from django.contrib import admin, messages
from django.db import models
from django.forms import RadioSelect
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.safestring import mark_safe
from .models import Animal, Category
from .forms import SetCategoryForm


admin.site.site_header = "Dierenwelzijn Voor vragen kunt u mailen naar: [email protected]"
admin.site.index_title = "Dierenwelzijn"
admin.site.site_title = "Dierenwelzijn"


class AnimalAdmin(admin.ModelAdmin):
    fields = ['name', 'sort', 'category', 'klasse_name', 'cites', 'uis', 'pet_list',
              'description', "feeding", "housing", "care", "literature", 'images', 'img_preview']
    readonly_fields = ['img_preview', 'klasse_name']
    autocomplete_fields = ['category']

    list_display = ('name', 'category_link', 'description')

    search_fields = ['name', 'category__name']
    search_help_text = "Zoek in de namen of families"
    actions = ['set_category']
    action_form = SetCategoryForm
    ordering = ['id']
    formfield_overrides = {
        models.BooleanField: {"widget": RadioSelect(
            choices=[(True, "Ja"), (False, "Nee")])}
    }

    def response_add(self, request, obj, post_url_continue=None):
        if '_addanother' in request.POST:
            url = reverse("admin:DierenWelzijnAdmin_animal_add")
            category_id = request.POST['category']
            qs = '?category=%s' % category_id
            return HttpResponseRedirect(''.join((url, qs)))
        if '_continue' in request.POST:
            return super(AnimalAdmin, self).response_add(request, obj, post_url_continue)
        else:
            return HttpResponseRedirect(reverse("admin:DierenWelzijnAdmin_animal_changelist"))

    def response_change(self, request, obj, post_url_continue=None):
        if '_addanother' in request.POST:
            url = reverse("admin:DierenWelzijnAdmin_animal_add")
            category_id = request.POST['category']
            qs = '?category=%s' % category_id
            return HttpResponseRedirect(''.join((url, qs)))
        if '_continue' in request.POST:
            return super(AnimalAdmin, self).response_change(request, obj)
        else:
            return HttpResponseRedirect(reverse("admin:DierenWelzijnAdmin_animal_changelist"))

    @admin.action(description="Zet de familie van de geselecteerde dieren")
    def set_category(self, request, queryset):
        category = request.POST['familie']
        queryset.update(category=category)
        messages.success(
            request, '{0} animal(s) were updated'.format(queryset.count()))

    def category_link(self, obj):
        return mark_safe('<a href="{}">{}</a>'.format(
            reverse("admin:DierenWelzijnAdmin_category_change",
                    args=[obj.category.pk]),
            obj.category.name
        ))
    category_link.short_description = 'Familie'


class CategoryAdmin(admin.ModelAdmin):
    fields = ['name', 'category', 'description', 'images', 'img_preview']
    readonly_fields = ['img_preview']
    autocomplete_fields = ['category']

    list_display = ('name', 'category_link',
                    'subcategories_link', 'animals_link')
    search_fields = ['name', 'category__name']
    search_help_text = "Zoek in de namen of families"
    actions = ['set_category']
    action_form = SetCategoryForm
    ordering = ['id']

    def response_add(self, request, obj, post_url_continue=None):
        if '_addanother' in request.POST:
            url = reverse("admin:DierenWelzijnAdmin_category_add")
            category_id = request.POST['category']
            qs = '?category=%s' % category_id
            return HttpResponseRedirect(''.join((url, qs)))
        if '_continue' in request.POST:
            return super(CategoryAdmin, self).response_add(request, obj, post_url_continue)
        else:
            return HttpResponseRedirect(reverse("admin:DierenWelzijnAdmin_category_changelist"))

    def response_change(self, request, obj, post_url_continue=None):
        if '_addanother' in request.POST:
            url = reverse("admin:DierenWelzijnAdmin_category_add")
            category_id = request.POST['category']
            qs = '?category=%s' % category_id
            return HttpResponseRedirect(''.join((url, qs)))
        if '_continue' in request.POST:
            return super(CategoryAdmin, self).response_change(request, obj)
        else:
            return HttpResponseRedirect(reverse("admin:DierenWelzijnAdmin_category_changelist"))

    @admin.action(description="Zet de familie van de geselecteerde families")
    def set_category(self, request, queryset):
        category = request.POST['familie']
        queryset.update(category=category)
        messages.success(
            request, '{0} categories were updated'.format(queryset.count()))

    def category_link(self, obj):
        if obj.category:
            return mark_safe('<a href="{}">{}</a>'.format(
                reverse("admin:DierenWelzijnAdmin_category_change",
                        args=[obj.pk]),
                obj.category
            ))
        return None
    category_link.short_description = 'Familie'

    def subcategories_link(self, obj):
        if obj.subcategories.exists():
            return mark_safe('<a href="{}">{}</a>'.format(
                reverse("admin:DierenWelzijnAdmin_category_changelist") +
                f"?category={obj.id}",
                "Subgroepen"
            ))
        return None
    subcategories_link.short_description = "Subgroepen"

    def animals_link(self, obj):
        if obj.animals.exists():
            return mark_safe('<a href="{}">{}</a>'.format(
                reverse("admin:DierenWelzijnAdmin_animal_changelist") +
                f"?category={obj.id}",
                "Dieren"
            ))
        return None
    animals_link.short_description = "Dieren in groep"


admin.site.register(Animal, AnimalAdmin)
admin.site.register(Category, CategoryAdmin)

Question: How to show the choosen user persmissions in the existing django app?
<p>I have a django app. And I try in the admin panel of django restrict user permissions with the AUTHENTICATION AND AUTHORIZATION tab.</p>
<p>But I noticed that something is missing in my existing django app on the AUTHENTICATION AND AUTHORIZATION tab.</p>
<p>Because I installed a clean install from django. And the AUTHENTICATION AND AUTHORIZATION tab looked like:</p>
<p><a href="https://i.sstatic.net/yqTGIQ0w.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/yqTGIQ0w.png" alt="enter image description here" /></a></p>
<p>and the AUTHENTICATION AND AUTHORIZATION tab in my existing django app looks like:</p>
<p><a href="https://i.sstatic.net/jbsfMFdJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/jbsfMFdJ.png" alt="enter image description here" /></a></p>
<p>So as you can see in my existing django app the right part is missing of the user permissions.</p>
<p>What I have done? Is checking the versions. And they are the same(clean version and exsting version)</p>
<p>I am using django version: 5.0.6. and the libraries</p>
<p>I checked the libraries installed with pip freeze of my exisiting version:</p>
<pre><code>anyio==4.2.0
asgiref==3.7.2
attrs==23.1.0
azure-common==1.1.28
azure-core==1.29.6
azure-storage-blob==12.19.0
azure-storage-common==2.1.0
certifi==2023.7.22
cffi==1.15.1
charset-normalizer==3.2.0
click==8.1.7
colorama==0.4.6
coreapi==2.3.3
coreschema==0.0.4
cryptography==39.0.0
defusedxml==0.7.1
Django==5.0.6
django-allauth==0.52.0
django-cors-headers==3.10.1
django-dotenv==1.4.2
django-filter==23.2
django-storages==1.14.2
djangorestframework==3.14.0
drf-spectacular==0.26.4
drf-yasg==1.20.0
exceptiongroup==1.2.0
gunicorn==21.2.0
h11==0.14.0
idna==3.4
inflection==0.5.1
isodate==0.6.1
itypes==1.2.0
Jinja2==3.1.2
jsonschema==4.19.0
jsonschema-specifications==2023.7.1
Markdown==3.4.4
MarkupSafe==2.1.3
oauthlib==3.2.2
packaging==23.1
Pillow==10.0.0
psycopg2==2.9.7
pycparser==2.21
PyJWT==2.6.0
pyrsistent==0.19.3
python-dateutil==2.8.2
python3-openid==3.2.0
pytz==2023.3.post1
PyYAML==6.0.1
referencing==0.30.2
requests==2.31.0
requests-oauthlib==1.3.1
rpds-py==0.10.2
ruamel.yaml==0.17.32
ruamel.yaml.clib==0.2.7
six==1.16.0
sniffio==1.3.0
sqlparse==0.4.4
typing_extensions==4.7.1
tzdata==2023.3
uritemplate==4.1.1
urllib3==2.0.4
uvicorn==0.23.2
waitress==2.1.2
</code></pre>
<p>And part of settings.py file of the existing app:</p>
<pre><code>LOGOUT_REDIRECT_URL = "/"
ACCOUNT_LOGOUT_REDIRECT_URL = "/"


REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',

'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),

'SWAGGER_SETTINGS': {
'LOGIN_URL': 'rest_framework:login',
'LOGOUT_URL': 'rest_framework:logout'
}


}


# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'CET'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/


STATIC_URL = '/static/'
MEDIA_URL = '/media/'

STATIC_DIRS = [
(BASE_DIR, 'static')
]
STATICFILES_DIRS = [BASE_DIR / 'static']


STATIC_ROOT = BASE_DIR / 'staticfiles/'
MEDIA_ROOT = BASE_DIR / 'media'
</code></pre>
<p>admin.py looks:</p>
<pre><code>from django.contrib import admin, messages
from django.db import models
from django.forms import RadioSelect
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.safestring import mark_safe
from .models import Animal, Category
from .forms import SetCategoryForm


admin.site.site_header = "Dierenwelzijn Voor vragen kunt u mailen naar: [email protected]"
admin.site.index_title = "Dierenwelzijn"
admin.site.site_title = "Dierenwelzijn"


class AnimalAdmin(admin.ModelAdmin):
fields = ['name', 'sort', 'category', 'klasse_name', 'cites', 'uis', 'pet_list',
'description', "feeding", "housing", "care", "literature", 'images', 'img_preview']
readonly_fields = ['img_preview', 'klasse_name']
autocomplete_fields = ['category']

list_display = ('name', 'category_link', 'description')

search_fields = ['name', 'category__name']
search_help_text = "Zoek in de namen of families"
actions = ['set_category']
action_form = SetCategoryForm
ordering = ['id']
formfield_overrides = {
models.BooleanField: {"widget": RadioSelect(
choices=[(True, "Ja"), (False, "Nee")])}
}

def response_add(self, request, obj, post_url_continue=None):
if '_addanother' in request.POST:
url = reverse("admin:DierenWelzijnAdmin_animal_add")
category_id = request.POST['category']
qs = '?category=%s' % category_id
return HttpResponseRedirect(''.join((url, qs)))
if '_continue' in request.POST:
return super(AnimalAdmin, self).response_add(request, obj, post_url_continue)
else:
return HttpResponseRedirect(reverse("admin:DierenWelzijnAdmin_animal_changelist"))

def response_change(self, request, obj, post_url_continue=None):
if '_addanother' in request.POST:
url = reverse("admin:DierenWelzijnAdmin_animal_add")
category_id = request.POST['category']
qs = '?category=%s' % category_id
return HttpResponseRedirect(''.join((url, qs)))
if '_continue' in request.POST:
return super(AnimalAdmin, self).response_change(request, obj)
else:
return HttpResponseRedirect(reverse("admin:DierenWelzijnAdmin_animal_changelist"))

@admin.action(description="Zet de familie van de geselecteerde dieren")
def set_category(self, request, queryset):
category = request.POST['familie']
queryset.update(category=category)
messages.success(
request, '{0} animal(s) were updated'.format(queryset.count()))

def category_link(self, obj):
return mark_safe('<a href="{}">{}</a>'.format(
reverse("admin:DierenWelzijnAdmin_category_change",
args=[obj.category.pk]),
obj.category.name
))
category_link.short_description = 'Familie'


class CategoryAdmin(admin.ModelAdmin):
fields = ['name', 'category', 'description', 'images', 'img_preview']
readonly_fields = ['img_preview']
autocomplete_fields = ['category']

list_display = ('name', 'category_link',
'subcategories_link', 'animals_link')
search_fields = ['name', 'category__name']
search_help_text = "Zoek in de namen of families"
actions = ['set_category']
action_form = SetCategoryForm
ordering = ['id']

def response_add(self, request, obj, post_url_continue=None):
if '_addanother' in request.POST:
url = reverse("admin:DierenWelzijnAdmin_category_add")
category_id = request.POST['category']
qs = '?category=%s' % category_id
return HttpResponseRedirect(''.join((url, qs)))
if '_continue' in request.POST:
return super(CategoryAdmin, self).response_add(request, obj, post_url_continue)
else:
return HttpResponseRedirect(reverse("admin:DierenWelzijnAdmin_category_changelist"))

def response_change(self, request, obj, post_url_continue=None):
if '_addanother' in request.POST:
url = reverse("admin:DierenWelzijnAdmin_category_add")
category_id = request.POST['category']
qs = '?category=%s' % category_id
return HttpResponseRedirect(''.join((url, qs)))
if '_continue' in request.POST:
return super(CategoryAdmin, self).response_change(request, obj)
else:
return HttpResponseRedirect(reverse("admin:DierenWelzijnAdmin_category_changelist"))

@admin.action(description="Zet de familie van de geselecteerde families")
def set_category(self, request, queryset):
category = request.POST['familie']
queryset.update(category=category)
messages.success(
request, '{0} categories were updated'.format(queryset.count()))

def category_link(self, obj):
if obj.category:
return mark_safe('<a href="{}">{}</a>'.format(
reverse("admin:DierenWelzijnAdmin_category_change",
args=[obj.pk]),
obj.category
))
return None
category_link.short_description = 'Familie'

def subcategories_link(self, obj):
if obj.subcategories.exists():
return mark_safe('<a href="{}">{}</a>'.format(
reverse("admin:DierenWelzijnAdmin_category_changelist") +
f"?category={obj.id}",
"Subgroepen"
))
return None
subcategories_link.short_description = "Subgroepen"

def animals_link(self, obj):
if obj.animals.exists():
return mark_safe('<a href="{}">{}</a>'.format(
reverse("admin:DierenWelzijnAdmin_animal_changelist") +
f"?category={obj.id}",
"Dieren"
))
return None
animals_link.short_description = "Dieren in groep"


admin.site.register(Animal, AnimalAdmin)
admin.site.register(Category, CategoryAdmin)

</code></pre>
<p>Question: How to show the choosen user persmissions in the existing django app?</p>
 
Top