Commit 9460a295 by Patryk Czarnik

rozwiązania kalkulatora

parent 24074499
<!DOCTYPE html>
{% load static %}
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Kalkulator</title>
<link rel="stylesheet" type="text/css" href="{% static 'styl.css'%}"/>
</head>
<body>
<h1>Kalkulator ze szkolenia</h1>
<p>Podaj dwie liczby i wybierz działanie:</p>
<form>
<input type="text" name="arg1">
<select name="operacja">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="^">^</option>
</select>
<input type="text" name="arg2"><br>
<button>Oblicz</button>
</form>
<div class="wynik">
{{wynik}}
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
{% load static %}
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Kalkulator</title>
<link rel="stylesheet" type="text/css" href="{% static 'styl.css'%}"/>
</head>
<body>
<h1>Kalkulator ze szkolenia</h1>
<p>Podaj dwie liczby i wybierz działanie:</p>
<form method="post">
{% csrf_token %}
<input type="number" name="arg1">
<select name="operacja">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="^">^</option>
</select>
<input type="number" name="arg2"><br>
<button>Oblicz</button>
</form>
{% if wynik %}
<div class="wynik">
{{request.POST.arg1}} {{request.POST.operacja}} {{request.POST.arg2}} = <strong>{{wynik}}</strong>
</div>
{% endif %}
{% if error %}
<div class="error">
{{error}}
</div>
{% endif %}
</body>
</html>
\ No newline at end of file
...@@ -8,7 +8,7 @@ def hello(request): ...@@ -8,7 +8,7 @@ def hello(request):
def czas_txt(request): def czas_txt(request):
return HttpResponse(str(datetime.now())) return HttpResponse(str(datetime.now()), content_type='text/plain')
def czas_html(request:HttpRequest) -> HttpResponse: def czas_html(request:HttpRequest) -> HttpResponse:
...@@ -37,8 +37,66 @@ def index(request:HttpRequest) -> HttpResponse: ...@@ -37,8 +37,66 @@ def index(request:HttpRequest) -> HttpResponse:
def rozmowa(request:HttpRequest) -> HttpResponse: def rozmowa(request:HttpRequest) -> HttpResponse:
imie = request.GET.get('imie', 'Anonim') # request.GET zwraca słownik wszystkich parametrów przekazanych w zapytaniu (jako parametry typu GET, czyli w adresie)
powitanie = f'Witaj {imie}!' # wartość ze słownika moglibyśmy odczytać pisząc request.GET['imie'], ale w razie braku danych byłby wyjątek KeyError
# można użyć metody get, która w razie braku danych zwraca None i nie wyrzuca błędu
imie = request.GET.get('imie')
if imie:
powitanie = f'Witaj {imie}!'
else:
powitanie = 'Witaj anonimie. Możesz swoje imię...'
return render(request=request, return render(request=request,
template_name='rozmowa.html', template_name='rozmowa.html',
context={'powitanie': powitanie}) context={'powitanie': powitanie})
def kalkulator_simple(request:HttpRequest) -> HttpResponse:
liczba1 = int(request.GET.get('arg1', '0'))
liczba2 = int(request.GET.get('arg2', '0'))
wynik = liczba1 + liczba2
return render(request=request,
template_name='kalkulator_get.html',
context={'wynik': wynik})
def kalkulator_get(request:HttpRequest) -> HttpResponse:
parametr1 = request.GET.get('arg1')
parametr2 = request.GET.get('arg2')
operacja = request.GET.get('operacja')
wynik = 0
if parametr1 and parametr2 and operacja:
liczba1 = int(parametr1)
liczba2 = int(parametr2)
wynik = oblicz(liczba1, liczba2, operacja)
return render(request=request,
template_name='kalkulator_get.html',
context={'wynik': wynik})
def kalkulator_post(request:HttpRequest) -> HttpResponse:
context = {}
try:
liczba1 = int(request.POST['arg1'])
liczba2 = int(request.POST['arg2'])
operacja = request.POST['operacja']
context['wynik'] = oblicz(liczba1, liczba2, operacja)
except KeyError:
pass
except ValueError as e:
context['error'] = f'Niepoprawne dane: {e}'
return render(request=request,
template_name='kalkulator_post.html',
context=context)
def oblicz(liczba1:int, liczba2:int, operacja:str) -> int:
if operacja == '+': return liczba1 + liczba2
if operacja == '-': return liczba1 - liczba2
if operacja == '*': return liczba1 * liczba2
if operacja == '/': return liczba1 // liczba2
if operacja == '^': return liczba1 ** liczba2
return 0
\ No newline at end of file
...@@ -26,4 +26,6 @@ urlpatterns = [ ...@@ -26,4 +26,6 @@ urlpatterns = [
path("czas.html", czas_html), path("czas.html", czas_html),
path("czas_szablon", czas_szablon), path("czas_szablon", czas_szablon),
path("rozmowa", rozmowa), path("rozmowa", rozmowa),
path("kalkulator_get", kalkulator_get),
path("kalkulator_post", kalkulator_post),
] ]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment