Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
python_django_2025
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
python_django_2025
Commits
6b83accc
Commit
6b83accc
authored
May 15, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
odczyt czasu - wersja z szablonem
parent
59bc72b1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
2 deletions
+38
-2
wyswietl_czas.html
aplikacja/templates/wyswietl_czas.html
+16
-0
views.py
aplikacja/views.py
+21
-2
urls.py
webowy/urls.py
+1
-0
No files found.
aplikacja/templates/wyswietl_czas.html
0 → 100644
View file @
6b83accc
<!DOCTYPE html>
<html
lang=
"pl"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Tytuł strony
</title>
</head>
<body>
<h1>
Informacja o bieżącym czasie
</h1>
<p>
Zaraz powiem, która jest godzina…
</p>
<p>
{{dt}}
</p>
<p>
Data: {{data}}
</p>
<p>
Czas: {{czas}}
</p>
<p>
Wersja sformatowana:
<strong>
{{fmt}}
</strong></p>
<p>
Mamy rok {{dt.year}}, jest teraz godzina {{dt.hour}}.
</p>
</body>
</html>
aplikacja/views.py
View file @
6b83accc
from
django.http
import
HttpResponse
from
django.http
import
HttpRe
quest
,
HttpRe
sponse
from
django.shortcuts
import
render
from
datetime
import
datetime
...
...
@@ -14,7 +14,7 @@ def podaj_czas(request):
# prosty sposób, aby zwrócić treść w formacie HTML
# po prostu tworzymy HTML w funkcji Pythona i wstawiamy do HttpResponse
def
czas_html
(
request
)
:
def
czas_html
(
request
:
HttpRequest
)
->
HttpResponse
:
godzina
=
datetime
.
now
()
.
strftime
(
'
%
H:
%
M:
%
S'
)
html
=
f
'''<html><head>
<title>Która godzina</title>
...
...
@@ -25,3 +25,22 @@ def czas_html(request):
</html>
'''
return
HttpResponse
(
html
,
content_type
=
'text/html;charset=UTF-8'
)
# Bardziej profesjonalnym sposobem tworzeia treści HTML jest używanie szablonów (template)
# czyli zewnętrznych plików, które zawierają treść do wysłania, ale pozwalają także
# wstawić wartości pochodzące z aplikacji.
def
czas_szablon
(
request
:
HttpRequest
)
->
HttpResponse
:
data_i_czas
=
datetime
.
now
()
data
=
data_i_czas
.
strftime
(
'
%
Y-
%
m-
%
d'
)
czas
=
data_i_czas
.
strftime
(
'
%
H:
%
M:
%
S'
)
fmt
=
data_i_czas
.
strftime
(
'
%
d.
%
m.
%
Y
%
H:
%
M'
)
return
render
(
request
,
'wyswietl_czas.html'
,
context
=
{
'dt'
:
data_i_czas
,
'data'
:
data
,
'czas'
:
czas
,
'fmt'
:
fmt
,
'wynik'
:
2
+
3
})
webowy/urls.py
View file @
6b83accc
...
...
@@ -25,4 +25,5 @@ urlpatterns = [
path
(
"hello"
,
hello
),
path
(
"czas"
,
podaj_czas
),
path
(
"czas_html"
,
czas_html
),
path
(
"czas_szablon"
,
czas_szablon
),
]
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