H a n d s o n, p r o j e c t b a s e d


Download 4.21 Mb.
Pdf ko'rish
bet321/344
Sana31.01.2024
Hajmi4.21 Mb.
#1818553
1   ...   317   318   319   320   321   322   323   324   ...   344
Bog'liq
Python Crash Course, 2nd Edition

The new_topic Template
Now we’ll make a new template called new_topic.html to display the form we 
just created.


User Accounts
413
{% extends "learning_logs/base.html" %}
{% block content %}
Add a new topic:
u

v
{% csrf_token %}
w
{{ form.as_p }}
x


{% endblock content %}
This template extends base.html, so it has the same base structure as 
the rest of the pages in Learning Log. At u we define an HTML form. 
The 
action
argument tells the browser where to send the data submitted 
in the form; in this case, we send it back to the view function 
new_topic()

The 
method
argument tells the browser to submit the data as a POST 
request.
Django uses the template tag 
{% csrf_token %}
v to prevent attackers 
from using the form to gain unauthorized access to the server (this kind 
of attack is called a cross-site request forgery). At w we display the form; here 
you see how simple Django can make certain tasks, such as displaying a 
form. We only need to include the template variable 
{{ form.as_p }}
for 
Django to create all the fields necessary to display the form automatically. 
The 
as_p
modifier tells Django to render all the form elements in para-
graph format, as a simple way to display the form neatly.
Django doesn’t create a submit button for forms, so we define one at x.
Linking to the new_topic Page
Next, we include a link to the 
new_topic
page on the 
topics
page:
{% extends "learning_logs/base.html" %}
{% block content %}
Topics

    --snip--

Add a new topic
{% endblock content %}
Place the link after the list of existing topics. Figure 19-1 shows the 
resulting form. Use the form to add a few new topics of your own.
new_topic.html
topics.html



Download 4.21 Mb.

Do'stlaringiz bilan baham:
1   ...   317   318   319   320   321   322   323   324   ...   344




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling