3

Let's imagine this model:

class ScribPart(models.Model):
  name = models.CharField(max_length=50, unique=True)
  text = models.TextField()

I'd like to attach a specific class to the field text and call a specific js file. So that the admin page would be like:

...
<script type="text/javascript" src="/static/js/mymarkup.js"></script>
...
<textarea id="id_text" name="text" class="mymarkup"></textarea>
...

How can I do that with a widget and/or custom admin form ?

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307

3 Answers3

5

To insert a <script> in an admin page the simplest thing to do is:

class ScribPartAdmin(model.ModelAdmin):
    ...
    your normal stuff...
    ...

    class Media:
        js = ('/path/to/your/file.js',)

ModelAdmin media definitions documentation

Now to add the class attribute to the textarea I think the simplest way to do it is like this:

from django import forms

class ScribPartAdmin(model.ModelAdmin):
    ...
    your normal stuff...
    ...

    class Meta:
        widgets = {'text': forms.Textarea(attrs={'class': 'mymarkup'})}

Overriding the default widgets documentation

I should add that this approach is good for a one shot use. If you want to reuse your field or JS many times, there's better ways to do it (custom widget for the field, with JS file specified if the JS is exclusively related to the field, extending template to include a JS file at many places).

Etienne
  • 12,440
  • 5
  • 44
  • 50
  • That seems to be the answer I was expecting. I have registered ScribPartAdmin with my model but the class is not here nor the javascript. – Pierre de LESPINAY Aug 12 '11 at 07:58
  • I have hard time helping you without seeing the code. A few notes: The script tag will only be present on the edit view, not on the list view. Check the HTML output to be sure that there's no script tag, maybe the script tag is there but with a wrong path so th JS file is not loaded. – Etienne Aug 12 '11 at 16:48
2

You have to create a template, put it in templates/admin/change_form_scribpart.html with this content:

{% extends "admin/change_form.html" %}
{% load i18n %}

{% block content %}
    <script type="text/javascript" src="/static/js/mymarkup.js"></script>
    {{ block.super }}
{% endblock %}

Also, don't forget to activate this new admin template in your ScribPart ModelAdmin:

class ScribPartAdmin(admin.ModelAdmin):   
    ordering = ...
    fieldsets = ...
    change_form_template = "admin/change_form_scribpart.html"
Gabriel Ross
  • 5,168
  • 1
  • 28
  • 30
  • If it's just for the javascript, the solution of Etienne seems simpler. However this is interesting, if I do that do I have to specify `change_form_template` if I put the template into the good directory (`/templates/admin/my_app/scribpart/change_form.html`) ? – Pierre de LESPINAY Aug 12 '11 at 08:19
  • Yes, then you set change_form_template = "admin/my_app/scribpart/change_form.html" – Gabriel Ross Aug 12 '11 at 12:48
0

You can send your form with json pack and get(check) with this code

results = ScribPart.all()
    for r in results :

        if r.test == id_text:
            self.response.out.write("<script type='text/javascript' src='/static/js/"+r.name+"mymarkup.js'></script>")
hicay
  • 78
  • 1
  • 2
  • 8