Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kurs_alx_pcz
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
kurs_alx_pcz
Commits
9460a295
Commit
9460a295
authored
Dec 12, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rozwiązania kalkulatora
parent
24074499
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
135 additions
and
2 deletions
+135
-2
kalkulator_get.html
mojprojekt/aplikacja/templates/kalkulator_get.html
+32
-0
kalkulator_post.html
mojprojekt/aplikacja/templates/kalkulator_post.html
+40
-0
views.py
mojprojekt/aplikacja/views.py
+61
-2
urls.py
mojprojekt/mojprojekt/urls.py
+2
-0
No files found.
mojprojekt/aplikacja/templates/kalkulator_get.html
0 → 100644
View file @
9460a295
<!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
mojprojekt/aplikacja/templates/kalkulator_post.html
0 → 100644
View file @
9460a295
<!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
mojprojekt/aplikacja/views.py
View file @
9460a295
...
@@ -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)
# 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}!'
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
mojprojekt/mojprojekt/urls.py
View file @
9460a295
...
@@ -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
),
]
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment