【django】【專題】(路由)django 路由表及反向解析
阿新 • • 發佈:2018-12-30
django 路由表及反向解析 – 個人草稿
錯誤訊息:The current path, <the url path>
, didn’t match any of these
一個常見的錯誤訊息如下:
一般這都是因為 urlpatterns = [ ...
中的內容可能因為筆誤/不熟悉,寫錯造成的。
常見原因
urlpatterns 中的路徑末尾沒有 “/”
注意下圖,瀏覽器路徑中,這個 url 末尾有 “/” 結束。
而上圖中的 9.
中的 url pattern 是沒有 “/” 結尾的。
如果瀏覽器中的 url 輸入沒有使用 “/” 結尾,則能夠正常解析出來。
但是如果 url pattern 使用了 “/” 結尾,則瀏覽器中對應該路徑的 url 不論有沒有以 “/” 結尾,django server 都可以解析。
urlpatterns 中的 /<int:pk>/
“:” 後帶有空格
這個原因就和我上面截圖中的 weibo/<int: pk>/crawl_and_display/
一樣(7.
那一行),
注意到 /<int:
後面帶有空格。
而 /<int:pk>/
中的 /<int:
和 pk>/
中間是不能用空格的!
其它/筆記
update weibo index href
[...]
<tbody>
{% for each_weibo in weibo_all_objects %}
<tr> <th scope="row">{{ each_weibo.id }}</th>
<td><a href="{% url 'weibo:detail' each_weibo.id %}"
class="">
{{ each_weibo.name }}</a>
</td>
<td><input type="button" class="btn btn-primary"
name="crawler" id="crawler" value="Crawl"
onclick="crawler({{ each_weibo.pk }})"></td>
{# onclick="window.open('{% url 'weibo:crawl_and_display' each_weibo.pk %}')"></td> #}
</tr>
{% endfor %}
</tbody>
</table>
<script>
function crawler(_pk){
var _url = `/weibo/${_pk}/crawl_and_display/`
window.open(_url);
}
</script>
</div>
對 line 11 使用
{# #}
註釋後,line 12 去掉註釋,該種方法也有效。