S
S
smsi2019-10-10 16:52:02
Django
smsi, 2019-10-10 16:52:02

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

2 answer(s)
G
gh0sty, 2019-10-11
@smsi

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);
});
...

By clicking on the currency (also), send an ajax request to the selected URL and save it in the session
. If anything, it will look something like this:
$('#eur_btn').on('click', function () {
    $.ajax({
        url : "/valutes/eur/",
        type : "post",
        data : $('#hidden-input-form').serialize()
    });
});
...

Where #hidden-input-form is a post method form consisting of only the hidden input "value". Krch, an invisible form with your dollars, etc. Don't forget the csrf_token!

T
tema_sun, 2019-10-10
@tema_sun

Users are anonymous, cookies are not an option.

What is it all of a sudden? Quite an option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question