Answer the question
In order to leave comments, you need to log in
Django - how to save the selection of one value from several into a variable in the html template and use it later?
Point in the right direction.
I'm learning Django.
There is a model with the price of goods in dollars and a conversion factor in euros and other currencies. Each product has its own coefficient stored in the database.
I would like to do this: in the html template, the user selects a currency, the value of the selected currency is stored in some variable.
The item's price is displayed in the selected currency, with the dollar price simply multiplied by the variable.
I don't understand how this can be done in Django.
Users are anonymous, cookies are not an option.
How to save the user's choice of currency in the current session?
What to use?
Thanks
Answer the question
In order to leave comments, you need to log in
First , make some currency the main one. In order not to be steamed with recalculations.
Secondly , pass your coefficients + base currency to the template (via the context).
Third , make the changes visual through JS.
For example, by clicking on a currency, change prices:
var price = {{price}};
var usd_to_eur = {{coeff.usd_to_eur}};
var usd_to_btc = {{coeff.usd_to_btc}};
$('#eur_btn').on('click', function () {
var new_price = price * usd_to_eur;
$('#price').text(new_price);
});
...
$('#eur_btn').on('click', function () {
$.ajax({
url : "/valutes/eur/",
type : "post",
data : $('#hidden-input-form').serialize()
});
});
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question