Skip to content

Instantly share code, notes, and snippets.

@rchrd2

rchrd2/admin.py

Last active Dec 26, 2020
Embed
What would you like to do?
Adding custom views to django's admin
from django.contrib import admin
from polls.models import Poll, Choice
from django.contrib.auth.models import User
from django.contrib.admin import AdminSite
from polls.views import index
class MyAdminSite(AdminSite):
def get_urls(self):
from django.conf.urls import url
urls = super(MyAdminSite, self).get_urls()
# Note that custom urls get pushed to the list (not appended)
# This doesn't work with urls += ...
urls = [
url(r'^my_view/$', self.admin_view(index))
] + urls
return urls
admin_site = MyAdminSite()
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 1
class UserInline(admin.StackedInline):
model = User
extra = 1
class PollAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
search_fields = ['question']
date_hierarchy = 'pub_date'
admin_site.register(Poll, PollAdmin)
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib.admin import AdminSite
from django.contrib import admin
admin.autodiscover()
from polls.views import index
from polls.admin import admin_site
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin_site.urls)),
)
from django.http import HttpResponse
from django.template import RequestContext, loader
from .models import Poll
def index(request):
template = loader.get_template('admin/base_site.html')
context = RequestContext(request, {})
return HttpResponse(template.render(context))
@blitzkriegcoding

This comment has been minimized.

Copy link

@blitzkriegcoding blitzkriegcoding commented May 30, 2016

Thanks for your help! My question is... How do you add the link to main menu of the admin? I need to add my view, I have the the view, the route and template, howerver how to do for integrate that to the admin menu?

@padymkoclab

This comment has been minimized.

Copy link

@padymkoclab padymkoclab commented Jul 3, 2016

How are you passed Context in own View? Where did you find it?

@joelalves

This comment has been minimized.

Copy link

@joelalves joelalves commented Feb 9, 2018

Django 1.11 and python 3
it's don't work for me
view image

@Barbichou

This comment has been minimized.

Copy link

@Barbichou Barbichou commented Apr 9, 2020

Thank you very much for your help,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment