-
-
Save hitme/8bab557bd6de44d6bf206c7dc1c30185 to your computer and use it in GitHub Desktop.
Adding custom views to django's admin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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