Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make new custom view at django admin

Tags:

python

django

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.

like image 434
Marsha Avatar asked Mar 08 '18 14:03

Marsha


People also ask

How do I change the admin template in Django?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.

Is Django's admin interface customizable if yes then how?

To implement it in your project, make a new app in your Django project named products. Install this application, type product in the INSTALLED_APPS list in settings.py file. We will now make models in the products app. The model will be used throughout the tutorial to customize the Django Admin.


1 Answers

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)
like image 111
Marsha Avatar answered Sep 20 '22 13:09

Marsha