Dino Geek, try to help you

How to customize the Django admin panel?


Django is a popular Python-Based framework for web development, renowned for its efficiency and speed. One of its amazing features is the admin interface, which is created automatically and can be customized. There are several ways to customize your Django admin interface, ranging from simple cosmetic changes to more extensive modifications. Here’s how:

1. Customizing the Admin Index Page: The prime way to customize it is by modifying the ‘admin/index.html’ template. Create a ‘templates/admin’ directory in any of your app directories and create an ‘index.html’ file there. Django will find and use your custom template in preference to the default one. Here, you can add some additional context to display more data, or complete rewrite the look and feel of the page.

1. Changing Fields Display in List: Suppose you have a model that has many fields, not all of which need be shown in the list display. You could override this using the `list_display` admin option. For instance, if we have a model ‘Book’ with fields ‘title’, ‘author’, ‘published\_date’, we could include only ‘title’ and ‘author’ in list display like so: \`\`\`python class BookAdmin(admin.ModelAdmin): list\_display = (‘title’, ‘author’) admin.site.register(Book, BookAdmin) \`\`\` This customizes the fields that are displayed for each instance of the model in the admin list page.

1. Customize Input Fields: Similarly, we can decide which fields are shown for form input, in which order, and whether or not they can be edited, using the `fields` and `readonly_fields` options. For example: \`\`\`python class BookAdmin(admin.ModelAdmin): readonly_fields=(‘published_date’,) fields = (‘title’, ‘author’, ‘published\_date’) \`\`\` In the example above, ‘published\_date’ will be displayed, but not editable.

1. Adding Search and Filter Options: For a more powerful admin interface, you can add search and filter options. This can be done using the `search_fields` and `list_filter` options. \`\`\`python class BookAdmin(admin.ModelAdmin): search_fields = [‘title’, ‘author_\_name’] list_filter = [‘published_date’] \`\`\`

1. Customizing ModelForm: In more advanced cases, you might want to represent a model’s field in a way that the built-in field widgets do not support. By specifying a custom `ModelForm` (which you’d create yourself), you can override how the fields of that form (and hence the model) are represented.

All examples illustrated above and more detailed descriptions can be located in the [Django official documentation for ‘ModelAdmin’](https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#modeladmin-options), where the `ModelAdmin` class is described. Remember that template lookup in Django is based on the [Django template lookup protocol](https://docs.djangoproject.com/en/3.1/topics/templates/#template-loading).

By following the documentation, you can highly customize your Django admin panel, making it powerful and efficient for managing your data.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use