How to modify Django CMS admin panel? - django

How may I widen the searchbox in Django CMS admin?
enter image description here

You can specify additional CSS to be loaded as part of the admin page.
For example, see below an example loading both CSS and JS files:
class ImageGalleryAssetAdmin(admin.ModelAdmin):
...
class Media:
js = ('admin/xxx/enhance_admin.js',)
css = {
'all': ('admin/css/xxx/list_view.css',)
}
admin.site.register(ImageGalleryAsset, ImageGalleryAssetAdmin)
You can then just find ID of your search box and specify CSS to change its looks.

Related

How can include a regular Django view content in a Wagtail page?

I have a website running on Django that uses Wagtail for most of the pages (that are simply text content, editable through the Wagtail Admin UI). However, I have some content that needs to be dynamically rendered from my own Django model (let's say it's a list of handouts). I'd like that content to be rendered on a Wagtail page that would also have some Wagtail models (e.g., top menu, some introductory text). What's the best way to do this? I thought about making the list of handouts an API endpoint, and rendering it with JavaScript + XHR, but it seems like there should be a good way to do it server-side.
Assuming the introductionary text is a block in a Wagtail StreamField, you could also define a Wagtail Block that links to a Django model. https://pypi.org/project/wagtail-modelchooser/ is a useful extension that provides this functionality. This makes it possible to render things from Django model instances in your Wagtail stream content.
One option would be to create a property on your Page model that retrieves whatever you are looking for from this other model
# rest of your imports
...
from handouts import Handouts
class MyPage(Page):
# rest of your page's fields
...
#property
def get_handouts(self):
handouts = Handouts.objects.all()
return handouts

Admin InlineForm add links to Detail Page of the Inline Model and add custom javascript

In Admin for InlineForm there is a link that goes to the website, be default the text is (View on site).
I want to add a similar link that goes to that model(that is inline) Detail Edit Page.
Insert a JavaScript script in one of the Edit/Details Model Page in Admin
How can this be done ?
Adding JS and CSS to a models list_view and form_view pages are easy.
Write a class Media inside the admin class for the respective model.
admin.py
class MyModelAdmin(admin.ModelAdmin):
class Media:
css = {
"all": ("css/mycss.css",)
}
js = ("js/my_js.js",)
Place the javascript file inside /yourapp/static/yourapp/js/my_js.js
and css inside /yourapp/static/yourapp/css/mycss.css

Django Admin - customize use jquery inside and custom fields

I want to implement in Django Admin a jquery plugin that "adjust" and image(http://guillotine.js.org/), them get the coordinates with ImageKit and save the new image.
I need some tutorials and advises how to do it.
I have no tutorials, but can give you advice.
You can customize your admin model with custom css and js, by Media class, like so:
class MyModelAdmin(admin.ModelAdmin):
class Media:
css = {
"all": ("my_styles.css",)
}
js = ("my_code.js",)
You can look in dev tools, how Django chose names id's and classes for elements in page and also check the docs.
Admin docs

How to unregister Mezzanine's Quick Blog from the Django admin dashboard?

I'm subclassing mezzanine's BlogPost and have unregistered it to prevent it from showing in the admin page. e.g.
class BlogPostAdmin(admin.ModelAdmin):
fieldsets = ((None, {"fields": ("title",)}),)
def in_menu(self):
for(name, items) in settings.ADMIN_MENU_ORDER
if "blog.BlogPost" in items:
return True
return False
admin.site.unregister(BlogPost)
admin.site.register(BlogPost, BlogPostAdmin)
However, this does not unregister the Quick Blog from the admin page.
How do I prevent quick blog from showing up in the admin page?
The docs don't explain how to do this(http://mezzanine.jupo.org/docs/admin-customization.html#dashboard)
Did you try to remove "blog_tags.quick_blog" from DASHBOARD_TAGS setting ?
inside your project create below folder structure
-templates -> admin -> includes
and inside includes folder create blank html called as quick_blog.html.
Thats it!
p.s "->" means folder inside folder mentioned before

Django grappelli adding custom js to change_form

I try to add my custom javascript to some change_form templates.
Before having installed Grappelli, I could do that by extending
change_form.html of admin and creating sub folders under the templates
with my module names.
For example my tree hireachy was like :
+templates
++admin_copies
+++change_form.html (I have added extra js blocks)
++admin
+++employer
++++employer
+++++change_form.html
++++employer_new
+++++change_form.html
As you understand I have a model named "Employer". I can add my custom
js methods under the change_form.html files.
However, this kind of inheritance cause javascript/breadcrumbs
problems with Grappelli.
How can I add simply my custom javascript functions for each model
separetly ?
Create a subclass of ModelAdmin, and define the custom js in its Media class.
admin.py, in the same folder as the models.py that has Employer:
from django.contrib import admin
from models import Employer
class EmployerAdmin(admin.ModelAdmin):
class Media:
js = ("js/custom.js",) # This paths are appended to your MEDIA_URL setting
admin.site.register(Employer, EmployerAdmin)
Read more here