Hi, I’m having trouble working out how to keep a selected currency by a user permanently submitted unless the user changes it themselves. I have read about using django sessions or adding the option tag in the html but struggling to find how to achieve this.
E.g. When the user logs in for the first time and they submit currency GBP, that selection will stay submitted unless they decide to submit another currency.
FYI - the user’s selection is not being stored in a model
VIEW
class MyDashboardView(TemplateView):
template_name = 'coinprices/my-dashboard.html'
def get(self, request, **kwargs):
form_c = CurrencyForm(prefix='form_c')
return render(request, self.template_name, {
'form_c': form_c,
})
def post(self, request):
currency = None
form_c = CurrencyForm(request.POST, prefix='form_c')
if request.method == 'POST':
if form_c.is_valid():
currency = form_c.cleaned_data['currency']
else:
form_c = CurrencyForm()
rates = {'USD': 1.0, 'AUD': 1.321, 'GBP': 0.764, 'CAD': 1.249}
deposit = 10000 / rates[currency]
context = {
'deposit': deposit,
'form_c': form_c,
}
return render(request, 'coinprices/my-dashboard.html', context)
FORM
class CurrencyForm(forms.Form):
currency = forms.ChoiceField(initial=('USD', 'USD'), choices=['USD', 'AUD', 'GBP' 'CAD'], label='Choose Currency:')
HTML
<span>
<form method="post">
{% csrf_token %}
{{ form_c.as_p }}
<button class="btn btn-outline-success btn-sm">Submit</button>
</form>
</span>
URL
urlpatterns = [
path('my-dashboard/', MyDashboardView.as_view(template_name='coinprices/my-dashboard.html'), name='dashboard'),
]