I'm trying to change a value based on the exchange rate selected by the user within the <option>
tag. I don't want to save the form result to the database. I just want it to dynamically change whenever the user changes the currency option.
Say a user selects a currency from the below form with the options in the view:
VIEW
def view(request):
currency_dict = {'EUR': 0.9154994049253867, 'JPY': 123.86706948640484, 'BGN': 1.7905337361530713, 'CZK': 22.375720955781375}
cur = currency_dict.items()
deposit = 10000
context = {
'currency_dict': currency_dict,
'cur': cur,
'deposit': deposit,
}
return render(request, 'coinprices/my-dashboard.html', context)
HTML - FORM
<div >
<div>
<span>
<h1>My Dashboard</h1>
</span>
<span>
<form method="post">
<label>Choose Currency:</label>
<input list="ex-currency">
<datalist id="ex-currency">
{% for k, v in currency %}
<option value="{{ k }}"></option>
{% endfor %}
</datalist>
{% csrf_token %}
<button >Submit</button>
</form>
</span>
</div>
</div>
Then based on the users selection, multiply the {{ deposit }}
value by the currency_dict[user_selected_currency]
and change the bold text (see below):
DESIRED OUTCOME
<span>
<div><b>Total Investment {{ user_selected_currency }}</b></div>
<span >${{ deposit * currency_dict[user_selected_currency] }}</span>
</span>
Any ideas how this can be achieved?
CodePudding user response:
Unfortunately, this is not a thing you can do with the render
function you will need a GET
request using ajax
when the user changes the value, and then you can call a function based on the user option and return a valid response to the user this called "Chained Dropdown List" I found this amazing article that maybe can help you How to Implement Dependent/Chained Dropdown List with Django