Ad N
Ad N

Reputation: 8386

How to define inline Javascript to be rendered for a Django form/widget/admin?

It is well documented how to add an external ressource file to be included by a page containing a Django form (or widget, or admin): defining a nested Media class with js or css members listing the approriate files.

This has the effect of adding include directive in the <head> section, of the form:

<script type="text/javascript" src="http://static.example.com/animations.js"></script>

Now, what we would need is to be able to define an inline script, one whose content is laid out directly in the <head> section, with the form:

<script type="text/javascript">
   alert("Dynamic string content");
</script>

The rationale is that the content of this script should be generated programatically.

How would one go about defining inline Javascript content for a ModelAdmin ?

Upvotes: 4

Views: 850

Answers (1)

Neil Twist
Neil Twist

Reputation: 1159

You could create a method in the view and set the file to be the url for that method? That way you can generate the js in the Django view.

Otherwise you'd have to override the class, or a field class and return the javascript to be rendered.

Upvotes: 1

Related Questions