DJANGO 1.1 dynamic url on static javascript

25 views
Skip to first unread message

lenin david

unread,
May 30, 2017, 12:23:33 PM5/30/17
to Django users
## Context
I am adding a static javascript file to my **ModelForm** on this way

forms.py

<!-- language: lang-python -->

    class SomeForm(forms.ModelForm):
    
        class Media:
          js = ('some-javascript.js',)

## Problem

On this javascript I need send a GET request to a rest endpoint hosted on same application using jquery, but i need the Server domain or a function to construct my url 

some-javascript.js,

<!-- language: lang-javascript -->

    django.jQuery.get('{% url "validate_username" %}')


## expect behavior

If I could create my url with django I could send the request without problems, Thanks in advance


lenin david

unread,
May 30, 2017, 12:23:33 PM5/30/17
to Django users
Context

I am adding a static javascript file to my **ModelForm** on this way

forms.py

    class SomeForm(forms.ModelForm):
   
       
class Media:
          js
= ('some-javascript.js',)


Problem

On this javascript I need send a GET request to a rest endpoint hosted on same application using jquery, but i need the Server domain or a function to construct my url 

some-javascript.js,

    django.jQuery.get('{% url "validate_username" %}')



ludovic coues

unread,
May 30, 2017, 4:48:54 PM5/30/17
to django...@googlegroups.com
Everything looking like {% %} or {{ }} are template tag. Your js file
are not processed by the templating engine so they can be served
really fast directly by your web server and not by django which is a
lot slower.

One solution would be to but the url in your html file. Add a <script>
tag sonewhere with `URL = { validate_username: "{% url
"validate_username" %}" }; `
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/99b1a2e3-879e-4395-98b4-52ba8d286bbc%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Ludovic Coues
+33 6 14 87 43 42
Reply all
Reply to author
Forward
0 new messages