{"id":75921,"date":"2020-01-20T12:33:13","date_gmt":"2020-01-20T07:03:13","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75921"},"modified":"2024-04-12T14:17:42","modified_gmt":"2024-04-12T08:47:42","slug":"creating-custom-admin-views","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/","title":{"rendered":"Creating Custom Admin Views"},"content":{"rendered":"<p>At this point, anyone looking to add custom <em>behavior<\/em> to Django\u2019s admin interface is probably starting to get a bit frustrated. \u201cAll you\u2019ve talked about is how to change the admin interface <em>visually<\/em>,\u201d we hear them cry. \u201cBut how do I change the way the admin interface <em>works<\/em>?\u201d The first thing to understand is that <em>it\u2019s not magic<\/em>. That is, nothing the admin interface does is \u201cspecial\u201d in any way \u2014 the admin interface is just a set of views (they live in django.contrib.admin.views) that manipulate data just like any other view.<\/p>\n<p>Sure, there\u2019s quite a bit of code in there; it has to deal with all the various options, field types, and settings that influence model behavior. Still, when you realize that the admin interface is just a set of views, adding custom admin views becomes easier to understand. By way of example, let\u2019s add a \u201cpublisher report\u201d view to our book application from Chapter 6. We\u2019ll build an admin view that shows the list of books broken down by publisher \u2014 a pretty typical example of a custom admin \u201creport\u201d view you might need to build. First, let\u2019s wire up a view in our URLconf. We need to insert this line:<\/p>\n<p>(r&#8217;^admin\/books\/report\/$&#8217;, &#8216;mysite.books.admin_views.report&#8217;),<\/p>\n<p><em>&nbsp;before<\/em> the line including the admin views. A bare-bones URLconf might look like this:<\/p>\n<p>from django.conf.urls.defaults import *<br>\nurlpatterns = patterns(&#8221;,<br>\n(r&#8217;^admin\/bookstore\/report\/$&#8217;, &#8216;bookstore.admin_views.report&#8217;),<br>\n(r&#8217;^admin\/&#8217;, include(&#8216;django.contrib.admin.urls&#8217;)),<br>\n)<\/p>\n<p>Why put the custom view <em>before<\/em> the admin inclusion? Recall that Django processes URL patterns in order. The admin inclusion matches nearly anything that falls under the inclusion point, so if we reverse the order of those lines, Django will find a built-in admin view for that pattern, which won\u2019t work. In this particular case, it will try to load a change list for a Report model in the books application, which doesn\u2019t exist. Now let\u2019s write our view. For the sake of simplicity, we\u2019ll just load all books into the context and let the template handle the grouping with the {% regroup %} tag. Create a file, books\/admin_views.py, with this code:<\/p>\n<p>from mysite.books.models import Book<br>\nfrom django.template import RequestContext<br>\nfrom django.shortcuts import render_to_response<br>\nfrom django.contrib.admin.views.decorators import staff_member_required<br>\ndef report(request):<br>\nreturn render_to_response(<br>\n&#8220;admin\/books\/report.html&#8221;,<br>\n{&#8216;book_list&#8217; : Book.objects.all()},<br>\nRequestContext(request, {}),<br>\n)<br>\nreport = staff_member_required(report)<\/p>\n<p>Because we left the grouping up to the template, this view is pretty simple. However, there are some subtle bits here worth making explicit:<\/p>\n<ul>\n<li>We use the staff_member_required decorator from contrib.admin.views.decorators. This is similar to the login_required decorator discussed in Chapter 12, but this decorator also checks that the given user is marked as a \u201cstaff\u201d member, and thus is allowed access to the admin interface.<\/li>\n<li>This decorator protects all the built-in admin views and makes the authentication logic for your view match the rest of the admin interface.<\/li>\n<li>We render a template located under admin\/. While this isn\u2019t strictly required, it\u2019s considered good practice to keep all your admin templates grouped in an admin We\u2019ve also put the template in a directory named books after our application \u2014 also a best practice.<\/li>\n<li>We use RequestContext as the third parameter (context_instance) to render_to_response. This ensures that information about the current user is available to the template.<\/li>\n<\/ul>\n<p>Finally, we\u2019ll make a template for this view. We\u2019ll extend the built-in admin templates to make this view visually appear to be part of the admin interface:<\/p>\n<p>{% extends &#8220;admin\/base_site.html&#8221; %}<br>\n{% block title %}List of books by publisher{% endblock %}<br>\n{% block content %}<br>\n&lt;div id=&#8221;content-main&#8221;&gt;<br>\n&lt;h1&gt;List of books by publisher:&lt;\/h1&gt;<br>\n{% regroup book_list|dictsort:&#8221;publisher.name&#8221; by publisher as books_by_publisher %}<br>\n{% for publisher in books_by_publisher %}<br>\n&lt;h3&gt;{{ publisher.grouper }}&lt;\/h3&gt;<br>\n&lt;ul&gt;<br>\n{% for book in publisher.list|dictsort:&#8221;title&#8221; %}<br>\n&lt;li&gt;{{ book }}&lt;\/li&gt;<br>\n{% endfor %}<br>\n&lt;\/ul&gt;<br>\n{% endfor %}<br>\n&lt;\/div&gt;<br>\n{% endblock %}<\/p>\n<p>By extending admin\/base_site.html, we get the look and feel of the Django admin \u201cfor free.\u201d Figure 17-2 shows what the end result looks like.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-67713\" src=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image028-3.jpg\" alt=\"image028\" width=\"496\" height=\"374\"><\/p>\n<p>Figure 17-2. A custom \u201cbooks by publisher\u201d admin view<\/p>\n<p>You can use this technique to add anything you can dream of to the admin interface. Remember that these so-called custom admin views are really just normal Django views; you can use all the techniques you learn in the rest of this book to provide as complex an admin interface as you need. We\u2019ll close out this chapter with some ideas for custom admin views.<\/p>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>At this point, anyone looking to add custom behavior to Django\u2019s admin interface is probably starting to get a bit frustrated. \u201cAll you\u2019ve talked about is how to change the admin interface visually,\u201d we hear them cry. \u201cBut how do I change the way the admin interface works?\u201d The first thing to understand is that&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8862],"class_list":["post-75921","page","type-page","status-publish","hentry","category-django-web-development","tag-creating-custom-admin-views"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating Custom Admin Views - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Custom Admin Views - Tutorial\" \/>\n<meta property=\"og:description\" content=\"At this point, anyone looking to add custom behavior to Django\u2019s admin interface is probably starting to get a bit frustrated. \u201cAll you\u2019ve talked about is how to change the admin interface visually,\u201d we hear them cry. \u201cBut how do I change the way the admin interface works?\u201d The first thing to understand is that...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:42+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image028-3.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/\",\"name\":\"Creating Custom Admin Views - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image028-3.jpg\",\"datePublished\":\"2020-01-20T07:03:13+00:00\",\"dateModified\":\"2024-04-12T08:47:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#primaryimage\",\"url\":\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image028-3.jpg\",\"contentUrl\":\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image028-3.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating Custom Admin Views\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating Custom Admin Views - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/","og_locale":"en_US","og_type":"article","og_title":"Creating Custom Admin Views - Tutorial","og_description":"At this point, anyone looking to add custom behavior to Django\u2019s admin interface is probably starting to get a bit frustrated. \u201cAll you\u2019ve talked about is how to change the admin interface visually,\u201d we hear them cry. \u201cBut how do I change the way the admin interface works?\u201d The first thing to understand is that...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:42+00:00","og_image":[{"url":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image028-3.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/","name":"Creating Custom Admin Views - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image028-3.jpg","datePublished":"2020-01-20T07:03:13+00:00","dateModified":"2024-04-12T08:47:42+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#primaryimage","url":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image028-3.jpg","contentUrl":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/07\/image028-3.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-custom-admin-views\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Creating Custom Admin Views"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75921"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75921\/revisions"}],"predecessor-version":[{"id":83435,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75921\/revisions\/83435"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}