7

Sorry, I am still new at django. I want to make custom view at admin site that is not related to my model. I have read the documentation (https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls), but does not work. Reading some tutorials does not work too... Here is what I tried:

admin.py

from django.contrib import admin
from django.urls import path
from .models import Question
from django.http import HttpResponse

class CustomAdminView(admin.ModelAdmin):
    def get_urls(self):
        urls = super().get_urls()
        my_urls = [
            path(r'^my_view/$', self.admin_site.admin_view(self.my_view))
        ]
        urls = my_urls + urls
        return urls


    def my_view(self, request):
        return HttpResponse("Hello, world.")


admin.site.register(Question)

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url

admin.autodiscover()
urlpatterns = [
    path(r'polls/',include('polls.urls')),
    path('admin/', admin.site.urls),
]

when I go to admin/my_view the result is 404 not found.

I tried by extending the AdminView too.

admin.py

from django.contrib.admin import AdminSite
from django.urls import path
from .models import Question
from django.http import HttpResponse

class CustomAdminView(AdminSite):
    def get_urls(self):
        urls = super().get_urls()
        my_urls = [
            path(r'my_view/', self.admin_view(self.my_view))
        ]
        urls = my_urls + urls
        return urls


    def my_view(self, request):
        return HttpResponse("Hello, world.")


custom_admin = CustomAdminView()
custom_admin.register(Question)

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from polls.admin import custom_admin

admin.autodiscover()
urlpatterns = [
    path(r'polls/',include('polls.urls')),
    path('admin/', custom_admin.urls),
]

I don't get 404 error on admin/my_view. But, the default models(user, and others) are not displayed. There is only my 'Question' model there. The previous one still has the default models.

How can I make the custom admin view with the right way? Thanks.

Marsha
  • 195
  • 2
  • 11
  • Your first snippet doesn't work because you don't use the CustomAdminView anywhere. – Daniel Roseman Mar 08 '18 at 14:59
  • sorry, but, I have no idea where to use it... When I tried to create new instance, I got error because I don't know what to pass into __init__ – Marsha Mar 08 '18 at 15:02
  • Django 2.1 [will have a hook](https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-the-default-admin-site) to override the default admin site easily. Until then, it's trickier. [This answer](https://stackoverflow.com/questions/40090090/django-1-10-override-adminsite) might help you. – Alasdair Mar 08 '18 at 16:19
  • Thanks!! I use the second snippet and register django's default models (users, models) which is written in that answer. – Marsha Mar 08 '18 at 16:48
  • *(users and groups) – Marsha Mar 08 '18 at 16:57

1 Answers1

5

It is solved. I am using my second admin.py and urls.py snippets and register django's default model, based on this answer: Django (1.10) override AdminSite

admin.py

from django.contrib.admin import AdminSite
from django.http import HttpResponse
from django.urls import path
from .models import Question
from django.contrib.auth.models import Group, User #add these moduls
from django.contrib.auth.admin import GroupAdmin, UserAdmin #and these

class CustomAdminView(AdminSite):
    def get_urls(self):
        urls = super().get_urls()
        my_urls = [
            path(r'my_view/', self.admin_view(self.my_view))
        ]
        urls = my_urls + urls
        return urls


    def my_view(self, request):
        return HttpResponse("Hello, world.")


custom_admin = CustomAdminView()
custom_admin.register(Question)

#register the default model

custom_admin.register(Group, GroupAdmin)
custom_admin.register(User, UserAdmin)
Marsha
  • 195
  • 2
  • 11