How Django Works? - 04

Haebin Ethan Jeong·2020년 6월 8일
0

Django

목록 보기
4/4

Write a minimal form

Updating our poll detail templace

  • The above template displays a radio button for each question choice. The value of each radio button is the associated question choice’s ID. The name of each radio button is "choice". That means, when somebody selects one of the radio buttons and submits the form, it’ll send the POST data choice=# where # is the ID of the selected choice.
  • We set the form’s action to {% url 'polls:vote' question.id %}, and we set method="post". Using method="post" (as opposed to method="get") is very important, because the act of submitting this form will alter data server-side. Whenever you create a form that alters data server-side, use method="post".
  • forloop.counter indicates how many times the for tag has gone through its loop
  • All POST forms that are targeted at internal URLs should use the {% csrf_token %} template tag.

Creating a Django view that handels the submitted data

  • request.POST is a dictionary-like object that lets you access submitted data by key name. In this case, request.POST['choice'] returns the ID of the selected choice, as a string. request.POST values are always strings.
  • We’re explicitly using request.POST in our code, to ensure that data is only altered via a POST call.
  • After incrementing the choice count, the code returns an HttpResponseRedirect rather than a normal HttpResponse. HttpResponseRedirect takes a single argument: the URL to which the user will be redirected.
  • reverse() call will return a string like '/polls/3/results/' where the 3 is the value of question.id. This redirected URL will then call the 'results' view to display the final page.
    - reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)
    • viewname can be a URL pattern name or the callable view object.

After somebody votes

After somebody votes in a question, the vote() view redirects to the results page for the question. Let’s write that view:

Using generic views: LESS CODE

Let’s convert our poll app to use the generic views system, so we can delete a bunch of our own code. We’ll have to take a few steps to make the conversion. We will:

  • Convert the URLconf.
  • Delete some of the old, unneeded views.
  • Introduce new views based on Django’s generic views.

Amending URLconf

  • Note that the name of the matched pattern in the path strings of the second and third patterns has changed from <question_id> to .

Amending Views

  • Next, we’re going to remove our old index, detail, and results views and use Django’s generic views instead. To do so, open the polls/views.py file and change it like so:
  • Using generic view: ListView and DetailView
    - Each generic view needs to know what model it will be acting upon. This is provided using the model attribute.
    - In, DetailView, The template_name attribute is used to tell Django to use a specific template name instead of the autogenerated default template name.
    - In ListView, it uses a default template called /_list.html; we use template_name to tell ListView to use our existing "polls/index.html" template.
profile
I'm a Junior studying Economics and Computer Science at Vanderbilt University.

0개의 댓글