I just read Elf Stenberg's solution on how to serve static Javascript with reversed URLs in Django, I was going to leave this as a comment but I better explain it here :P
When developing Django Widgets that require Javascript interaction with the server (Ajax, XHR, etc... ), you want your widget seamlessly deployable and have it know where to find its information (the endpoint).
Elf's solution is pretty interesting, as he runs the static fles through the Django Template renderer and stores the expanded versions for deployment. This is nice, but :
-
You have to re-render all the files if you change the endpoint
-
You have to re-render if you use the widget somewhere else (different proyect).
-
It becomes difficult to have an URL with parameters like a model Id or any variable (since there's no context).
What I do is add the URL to the Widget and set it as a document javascript variable, then use that variable on my .js
It goes something like this:
class MyWidget(forms.Widget):
def __init__(self, endpoing, *args, **kwargs):
self.endpoint = endpoint
super(self, MyWidget).__init__(*args, **kwargs)
def render(self):
output = super(self, MyWidget).render()
output =+ '<script>var WIDGET_ENDPOINT = "%s";