Challenge: Write your View
Note
For convenience, let's make the first view in one file with URL addresses (
urls.py
).The presented code for the task is part of the framework and cannot work separately, so errors will be received when you try to
Run Code
. Use theSubmit Task
button to solve this task.
Opgave
Swipe to start coding
Implement the index
view:
- Define the
html
variable that should contain Docstring. - Docstring should represent the HTML code for page, where you need to:
- define the
<body>
tag with the CSS propertybackground-color
with valuelightblue
; - write the first header (
<h1>
tag) with the contentDjango
; - define the second header (
<h2>
tag) with the contentPython in Web
. - define the paragraph (
<p>
) with thePython allows us to create web applications.
content.
- define the
- Return the
HttpResponse
object by passing thehtml
variable to this object.
Connect the index
view to the urlpatterns
:
- The URL endpoint should be named
home
. - Pass the
index
view to thepath
function as the second argument. - This URL pattern should be named
home-page
using thename
keyword.
Once you've completed this task, click the button below the code to check your solution.
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from django.urls import path
from django.http import HttpResponse
def index(request):
html = """
<body style='background-color: lightblue'>
<h1>Django</h1>
<p>I start learning Django!</p>
<h2>Python in Web</h2>
<p>Python allows us to create web applications.</p>
</body>
"""
return HttpResponse(html)
urlpatterns = [
path("home", index, name="home-page"),
]
Var alt klart?
Tak for dine kommentarer!
Sektion 2. Kapitel 4
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from django.urls import path
from django.http import HttpResponse
def index(___):
___ = """
<___ style='___: lightblue'>
<h1>___</h1>
<p>I start learning Django!</p>
___Python in Web___
___
</body>
"""
return ___(___)
urlpatterns = [
path("___", ___, name="___"),
]
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat