Django Tutorials :: Get and Post. Request - 24loader, Home of exclusive blog and update music and entertaiment

Django Tutorials :: Get and Post. Request

<
GET and POST Requests
The two main types of request you’ll use when building web apps are GET
requests and POST requests. You use GET requests for pages that only read
data from the server. You usually use POST requests when the user needs to
submit information through a form. We’ll be specifying the POST method
for processing all of our forms. (A few other kinds of requests exist, but we
won’t be using them in this project.)
The function new_topic() takes in the request object as a parameter.
When the user initially requests this page, their browser will send a GET
request. When the user has filled out and submitted the form, their browser
will submit a POST request. Depending on the request, we’ll know whether
the user is requesting a blank form (a GET request) or asking us to process
a completed form (a POST request).
The test at u determines whether the request method is GET or POST.
If the request method is not POST, the request is probably GET, so we need
to return a blank form (if it’s another kind of request, it’s still safe to return
a blank form). We make an instance of TopicForm v, store it in the vari￾able form, and send the form to the template in the context dictionary {.
Because we included no arguments when instantiating TopicForm, Django
creates a blank form that the user can fill out.
If the request method is POST, the else block runs and processes the
data submitted in the form. We make an instance of TopicForm w and pass
it the data entered by the user, stored in request.POST. The form object that’s
returned contains the information submitted by the user.
We can’t save the submitted information in the database until we’ve
checked that it’s valid x. The is_valid() function checks that all required
fields have been filled in (all fields in a form are required by default) and
that the data entered matches the field types expected—for example, that
the length of text is less than 200 characters, as we specified in models.py
in Chapter 18. This automatic validation saves us a lot of work. If every￾thing is valid, we can call save() y, which writes the data from the form
to the database. Once we’ve saved the data, we can leave this page. We
use reverse() to get the URL for the topics page and pass the URL to
HttpResponseRedirect() z, which redirects the user’s browser to the topics
page. On the topics page, the user should see the topic they just entered
in the list of topics.
The new_topic Template
Now we make a new template called new_topic.html to display the form we
just created:
new_topic.html {% extends "learning_logs/base.html" %}
{% block content %}
 <p>Add a new topic:</p
<form action="{% url 'learning_logs:new_topic' %}" method='post'>
v {% csrf_token %}
w {{ form.as_p }}
x <button name="submit">add topic</button>
 </form>

{% 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 server 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 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 paragraph format, which is
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:
topics.html {% extends "learning_logs/base.html" %}
{% block content %}
 <p>Topics</p>
 <ul>
 --snip--
 </ul>

 <a href="{% url 'learning_logs:new_topic' %}">Add a new topic:</a>
{% endblock content %}
Place the link after the list of existing topics. Figure 19-1 shows the
resulting form. Go ahead and use the form to add a few new topics of
your own.

No comments

Theme images by friztin. Powered by Blogger.
//]]>