Last active
March 3, 2022 00:14
-
-
Save rchrd2/c1cabcbfbfb27295a22c 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)) |
How are you passed Context in own View? Where did you find it?
Django 1.11 and python 3
it's don't work for me
view image
Thank you very much for your help,
Instead of doing admin_site.register(Poll, PollAdmin)
, you could also use the @admin.register()
decorator.
You can do @admin.register(Poll, site=admin_site)
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?