Overriding admin template in django to add custom javascript

Overriding admin template in django to add custom javascript



I have a model that I need to add some custom javascript processing to its admin form.



I have tried an implementation via the following guide:
https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#overriding-admin-templates



So I created my own change_form.html, and I overrode object-tools-items and put my js in there, but I'm not seeing it when I go to the change form. Then, just as a test, I put it directly into the real django change_form.html, but still nothing.


change_form.html


object-tools-items


change_form.html



Then to see if that template is being used, I changed it - added data, created syntax errors, but still, it had no effect. So it seems like that template isn't being used at all. I grepped for change_form.html to see where it's rendered from, and I found it in contrib/admin/options.py:render_change_form(), so I set a breakpoint there, but it was never hit. But the HTML sure looks like it came from that template.


change_form.html



Can anyone give me some direction here please?





Django debug toolbar will tell you exactly how a page is constructed, and about a million other indispensable things. github.com/django-debug-toolbar/django-debug-toolbar
– Rob L
Jan 21 '14 at 18:20






The django debug toolbar is definitely very cool and helpful. But when I installed it it also downloaded and installed django 1.6 (I was running 1.5) Luckily it installed it in a different place, but now my default version is 1.6 and I need to run 1.5. But thanks anyway, as I'm sure it will come in handy.
– Larry Martell
Jan 21 '14 at 19:17





install an older version or use pip install --upgrade-strategy="only-if-needed" to avoid auto upgrading dependencies when not necessary
– DylanYoung
Mar 15 '17 at 15:22


pip install --upgrade-strategy="only-if-needed"




1 Answer
1



You do not need to override admin templates to add your custom javascript to admin pages.



You can add your assets like this:



https://docs.djangoproject.com/en/dev/topics/forms/media/#assets-as-a-static-definition



And then you just need to override your forms that admin site uses.





Thanks! This looks like exactly what I need. I created a form to override the admin form: class MyCategoryAdminForm(forms.ModelForm): class Meta: model = Category class Media: js = ('CategoryAdmin.js') But instead of picking up CategoryAdmin.js it seems to split this and it looks for a file for each character: <script type="text/javascript" src="/static/C"></script> <script type="text/javascript" src="/static/a"></script> <script type="text/javascript" src="/static/t"></script> <script type="text/javascript" src="/static/e"></script> . .
– Larry Martell
Jan 21 '14 at 20:12






I figured that issue out - it wants a tuple for the js so adding a comma fixed that. Now I just have to get the script to work ;-)
– Larry Martell
Jan 21 '14 at 20:34





Good job, dude :)
– Odif Yltsaeb
Jan 21 '14 at 20:49



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?