#31209: Logic flaw in the tutorial 4.
-------------------------------------+-------------------------------------
     Reporter:  Saitama996           |                    Owner:  nobody
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  Documentation        |                  Version:  3.0
     Severity:  Normal               |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by felixxm):

 * status:  new => closed
 * resolution:   => invalid


Old description:

> The flaw in the [https://docs.djangoproject.com/en/3.0/intro/tutorial04/]
> where,
>
> Writing a form
>
> polls/templates/polls/detail.html
>
> {{{
> <h1>{{ question.question_text }}</h1>
>
> {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif
> %}
>
> <form action="{% url 'polls:vote' question.id %}" method="post">
> {% csrf_token %}
> {% for choice in question.choice_set.all %}
>     <input type="radio" name="choice" id="choice{{ forloop.counter }}"
> value="{{ choice.id }}">  <!-- This should be value={{choice.pk}} -->
>     <label for="choice{{ forloop.counter }}">{{ choice.choice_text
> }}</label><br>
> {% endfor %}
> <input type="submit" value="Vote">
> </form>
> }}}
>
> As you can see the value is passed as the choice id, however when it
> comes to the function,
>

> {{{
> def vote(request, question_id):
>     question = get_object_or_404(Question, pk=question_id)
>     try:
>         selected_choice =
> question.choice_set.get(pk=request.POST['choice'])
> }}}
>
>  here the selected_choice is picked upon the primary key, which is fine
> in this case, but in case there is a conflict between the id and pk, this
> wont work.

New description:

 The flaw in the [https://docs.djangoproject.com/en/3.0/intro/tutorial04/]
 where,

 Writing a form

 polls/templates/polls/detail.html

 {{{
 <h1>{{ question.question_text }}</h1>

 {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif
 %}

 <form action="{% url 'polls:vote' question.id %}" method="post">
 {% csrf_token %}
 {% for choice in question.choice_set.all %}
     <input type="radio" name="choice" id="choice{{ forloop.counter }}"
 value="{{ choice.id }}">  <!-- This should be value={{choice.pk}} -->
     <label for="choice{{ forloop.counter }}">{{ choice.choice_text
 }}</label><br>
 {% endfor %}
 <input type="submit" value="Vote">
 </form>
 }}}

 As you can see the value is passed as the choice id, however when it comes
 to the function,


 {{{
 def vote(request, question_id):
     question = get_object_or_404(Question, pk=question_id)
     try:
         selected_choice =
 question.choice_set.get(pk=request.POST['choice'])
 }}}

 here the selected_choice is picked upon the primary key, which is fine in
 this case, but in case there is a conflict between the id and pk, this
 wont work.

--

Comment:

 In `Question` and `Choice` models we don't have custom `id` fields that
 are not primary keys, so I don't see any value in changing this. We do the
 same for `question.id` in `get_object_or_404(Question, pk=question_id)`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31209#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.5e4e23837e1d1d67555fc21cce06c95f%40djangoproject.com.

Reply via email to