fixed AJAX request and response
This commit is contained in:
parent
fce2f77cb2
commit
9d6c4b308c
|
@ -3,5 +3,13 @@ from currencies.models import ExchangeRate_name
|
|||
|
||||
|
||||
class CurrencyForm(forms.Form):
|
||||
currencies = forms.ModelChoiceField(
|
||||
queryset=ExchangeRate_name.objects.all())
|
||||
# https://bradmontgomery.net/blog/2008/11/24/a-simple-django-example-with-ajax/
|
||||
currencies = forms.ModelChoiceField(
|
||||
queryset=ExchangeRate_name.objects.all())
|
||||
|
||||
CURRENCY_CHOICES = [(t.name, t.name) for t in
|
||||
ExchangeRate_name.objects.all()]
|
||||
|
||||
type = forms.ChoiceField(choices=CURRENCY_CHOICES,
|
||||
widget=forms.Select(attrs={
|
||||
'onchange': 'get_vehicle_color();'}))
|
||||
|
|
|
@ -12,16 +12,15 @@ from django.http import JsonResponse
|
|||
|
||||
def currency_update(request):
|
||||
# https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html
|
||||
if request.method == 'GET':
|
||||
print('its get method')
|
||||
if request.GET.get('currency_update', None) == 'CHF':
|
||||
data = {}
|
||||
else:
|
||||
print('its not get method')
|
||||
currency = request.GET.get('currency_update', None)
|
||||
data = ExchangeRate.objects.filter(
|
||||
name__name=currency).values(
|
||||
'exchange_rate_to_chf').latest(
|
||||
'date__date')
|
||||
print('currency: ', currency, 'data: ', data)
|
||||
currency = request.GET.get('currency_update', None)
|
||||
data = ExchangeRate.objects.filter(
|
||||
name__name=currency).values(
|
||||
'exchange_rate_to_chf').latest(
|
||||
'date__date')
|
||||
print('currency:', currency, 'data: ', data)
|
||||
return JsonResponse(data)
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
$("#currency_update").change(function () {
|
||||
$("#id_currency_update").change(function () {
|
||||
var currency_update = $(this).val();
|
||||
$("#id_currency_update").val(currency_update);
|
||||
$.ajax({
|
||||
url: '/ajax/currency_update/',
|
||||
data: {
|
||||
|
@ -7,7 +8,22 @@
|
|||
},
|
||||
dataType: 'json',
|
||||
success: function (data) {
|
||||
alert("es pop auf! --dies kommt von: static/js/app.js--.", data);
|
||||
alert("es pop auf! --dies kommt von: static/js/app.js--.");
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//document.getElementById('id_currency_update').getElementsByTagName('currency_update')
|
||||
//$("#id_currency_update").val('USD').selected = 'selected';
|
||||
|
||||
//https://stackoverflow.com/a/30489067/4061870
|
||||
// var obj = document.getElementById("id_currency_update");
|
||||
// for(i=0; i<obj.options.length; i++){
|
||||
// if(obj.options[i].value == "USD"){
|
||||
// obj.selectedIndex = i;
|
||||
// }
|
||||
// }
|
||||
|
||||
// var e document.getElementById("id_currency_update");
|
||||
//e.value = currency_update;
|
||||
|
|
|
@ -8,31 +8,35 @@
|
|||
<ul class="nav navbar-nav">
|
||||
<li><a href="#">CART</a></li>
|
||||
{% if user.is_authenticated %}
|
||||
<li>
|
||||
<a href="{% url 'profile' %}">PROFILE</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="{% url 'logout' %}">LOGOUT</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="{% url 'profile' %}">PROFILE</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="{% url 'logout' %}">LOGOUT</a>
|
||||
</li>
|
||||
{% else %}
|
||||
<li>
|
||||
<a href="{% url 'login' %}">LOGIN</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="{% url 'login' %}">LOGIN</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
<!-- https://pypi.python.org/pypi/django-select-multiple-field -->
|
||||
<form method="POST" novalidate>
|
||||
<select name="currency_update" id="id_currency_update">
|
||||
<option value="CHF">CHF</option>
|
||||
<option value="USD">USD</option>
|
||||
<option value="GBP">GBP</option>
|
||||
<option value="JPY">JPY</option>
|
||||
</select>
|
||||
{% csrf_token %}
|
||||
</form>
|
||||
</ul>
|
||||
<form action="" method="POST" novalidate>
|
||||
{% csrf_token %}
|
||||
<select name="currency_update" id="currency_update" onchange="this.form.submit()">
|
||||
<option value="CHF">CHF</option>
|
||||
<option value="USD">USD</option>
|
||||
<option value="GBP">GBP</option>
|
||||
<option value="JPY">JPY</option>
|
||||
</select>
|
||||
<ul class="nav navbar-nav">
|
||||
{{ form.as_li }}
|
||||
</ul>
|
||||
</form>
|
||||
</div>
|
||||
<!--
|
||||
Es wird auf id im app.js file gemached.
|
||||
die URL wird im app.js gesetzt
|
||||
und mit urls.py weitergereicht.
|
||||
dann auf name im views.py gemached und ausgeführt.
|
||||
-->
|
||||
</div>
|
||||
</nav>
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in New Issue