开发者

Django grappelli adding custom js to change_form

开发者 https://www.devze.com 2023-04-12 16:50 出处:网络
I try to add my custom javascript to some change_form templates. Before having installed Grappelli, I could do that by extending

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 Grappell开发者_如何学编程i. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号