uranusjr / django-tutorial-for-programmers

Chapter text and example code for the Django tutorial first appeared during ITHome Ironman 7
289 stars 93 forks source link

L17 template tag/filter 範例問題 #8

Closed kuanyui closed 9 years ago

kuanyui commented 9 years ago

做出template tag後,這個範例code:

{# stores/templates/stores/store_detail.html #}

{% load stores_tags %}

{% if store|deletable:user %}
<input type="submit" value="刪除" class="btn btn-danger">
{% endif %}

是不是省略了很多東西?因為這樣改是不能跑的(還想說為什麼這裡不需要csrf_token):

{% load stores_tags %}
......
{% block content %} 
  <a href="{% url 'store_edit' pk=store.pk %}" class="btn btn-default"> 編輯店家資訊 </a>

  <form method="post" action="{% url 'store_delete' pk=store.pk %}">
    {% if store|deletable:user %}
      {% csrf_token %}
      <!-- 為啥是input?! -->
    <input type="submit" class="btn btn-danger" value="刪除店家"></a>
    {% endif %}
  </form>
...
uranusjr commented 9 years ago

我當時想表達的應該是「只要在合適的地方加上這一段,就可以實作店家刪除」。不過確實好像省略太多東西了,有點想當然。我會再增加一些說明和註解讓它清楚一點。

<input type="submit"> 是實作 submit button 的其中一種方法。這和 <button type="submit"></button> 在作用上差不多,只是兩者在比較舊的瀏覽器裡行為略有不同,預設外觀也有差異。不過現在(不考慮 Windows XP 和舊 IE 的話)它們兩個基本上可以視為等價,尤其我們用了 Bootstrap,連外觀也看不出差異了。稍微比對了一下,我在其他地方好像都是用 button(?),不知道為什麼這邊會突然想這樣寫。有空應該稍微全部檢查一次把它統一比較好(或許加個 footnote 說明兩者的差異)。

感謝!

kuanyui commented 9 years ago

期待講解,沒碰過web開發,這部份完全看不懂 ˊ・ω・ˋ