Skip to main content

Posts

Showing posts with the label admin

Django adding custom widget to Django Admin

Sometimes you need to get out of standard behaviour of your Django admin. It means expanding its functionality with custom things. In our case we have a model that we want to add a custom html element. Button that must load something via AJAX from another place to be exact. We have a typical Django polls application for simplicity. Here is its structure: First of all we need to create a structure. So here comes our model: # models.py from django.db import models class Poll (models.Model): question = models.CharField(max_length= 200 ) pub_date = models.DateTimeField( 'date published' ) We have to have our django application admin configuration to display that model: # admin.py from django.contrib import admin from polls.models import Poll admin.site.register(Poll) It will look in Django admin like so: You can see polls available here in admin. You can do all the typical things with Polls model. Add/Delete/Edit... Using standard D...

Django: adding additional Admin static and writing JS snippets for Django Admin

It often gets that you need it because you invent something that is meant to be displayed for admin. E.g. some nasty form field or something. Here is my recipe to add some additional Django static to your admin. Rather than writing a widget , that is more proper way here... I was using a widget already (from another package). So I could not use it properly, at least without a ton of overrides and complexity it leads to. SO I've decided to add the JS functionality to override a lack of backend. First of all we should add the javascript/css like so: # admin.py class MyModelAdmin (admin.ModelAdmin): # admin additions class Media : css = { "all" : ( "css/my_style.css" ,) } js = ( "js/my_script.js" ,) This add should list your files, loaded in the admin . So you can access them and see in the Django admin frontend itself. Like so: Now that your script and style are loaded you could jus...

Django: Add Permission model to admin panel

How to add a permissions model to your Django admin? You may add those lines to anywhere in your code. But it is more convinient to add this to your admin.py or place where django admin custom code lies. Here are them: from django.contrib.auth.models import Permission admin.site.register(Permission) Now you can manage them in in your Django admin panel. May look like this: Hope this helps.