Skip to content

Instantly share code, notes, and snippets.

Forked from rchrd2/
Created December 26, 2020 13:55
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
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('')),
# 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment