4👍
You didn’t pass the value from javascript function to django template tag. But in this case you can use ajax calls.
http://www.tangowithdjango.com/book/chapters/ajax.html
https://bradmontgomery.net/blog/2008/11/24/a-simple-django-example-with-ajax/
Update:
See this you can understand what’s going on.
How to pass javascript variable to django custom filter
Hope this is helpful idea.
7👍
Django templates are build on the server side, at the generation of the page while JavaScript is executed on the client side, when it is needed. Thus, Django and Javascript can’t share objects/data.
In your page, with the current Javascript, you will have something like:
<script type="text/javascript">
function getOption(sel){
var country = sel.value;
// Empty line due to the templatetag
console.log('')
}
</script>
What you need is either generate the list in your view and return a carrier
object already constructed. With some luck, you might be able to use it in Javascript.
The best way here is still to make an AJAX request to get this list:
def get_available_carriers(request, weight, country, length, width, height):
url = 'http://www.sendfromchina.com/shipfee/web_service?wsdl'
client = Client(url)
rates = client.service.getRates(weight,country,length,width,height)
rates=map(lambda x: (x._shiptypecode, x._totalfee), rates)
return json.dumps(rates)
and the get it with jQuery (if you are using it):
$.get('{% url "get_available_carriers" 1 country 10 10 10 %}', function(data){
console.log(data);
});
Don’t forget to define the URL pattern, with the get_available_carriers
in my example.
- Django makemigrations not detecting project/apps/myapp
- Difficulty with Django and jQuery (why is $ undefined in the admin app?)
- Django, division between two annotate result won't calculate correctly
- Decoder JPEG not available error when following Django photo app tutorial