'How can I save a user's selected choice on refresh Django
I'm trying to save a user's choice which but on refresh or navigating to another page, the submission had been removed.
How can I save the user's submission so it won't reset/change unless the user chooses to?
I.e. if someone chooses AUD, that choice will remain submitted on refresh/navigation/login/logout.
FORM
class CurrencyForm(forms.Form):
currency = forms.ChoiceField(initial=('USD', 'USD'), choices=['USD', 'AUD', 'GBP' 'CAD'], label='Choose Currency:')
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 = 'USD'
form_c = CurrencyForm(request.POST, prefix='form_c')
if request.method == 'POST':
if form_c.is_valid():
currency = form_c.cleaned_data['currency']
rates = {'USD': 1.0, 'AUD': 1.321, 'GBP': 0.764, 'CAD': 1.249}
deposit = 10000 / rates[currency]
context = {
'deposit': deposit
}
return render(request, 'coinprices/my-dashboard.html', context)
HTML
<span>
<form method="post">
{% csrf_token %}
{{ form_c.as_p }}
<button class="btn btn-outline-success btn-sm">Submit</button>
</form>
</span>
<span class="text-xl">${{ deposit }}</span>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
