我在Django 1.8中使用帶有兩個應用程序的命名空間URL。請參見下面的簡約結構:在應用程序之間使用Django url模板標記
車
- 模板
- -Cart
- - -Cart-template.html
- urls.py
- views.py
店
- settings.py
- urls.py
產品
個 - 模板
- -Cart
- - -Cart-template.html
- urls.py
- views.py
所以我建立了我的路線,爲 '主' 的應用程序:
和車應用
#Cart/urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
]
這個模板標籤的工作原理PE rfectly:
# Cart/templates/cart/cart-template.html
<form action= {% url 'cart:index' %} method="post">
然而,當我是一個產品(按產品應用處理)的頁面上,點擊一個按鈕,應該重定向我在完全相同的方式,它提供了404:
# Product/templates/product/product-template.html
<form action= {% url 'cart:index' %} method="post">
換句話說,我不能在另一個應用程序Product的模板中使用購物車應用程序的命名空間url。我可以使這成爲可能/我做錯了什麼?這是我的輸出:
Page not found (404)
Request Method: GET
Request URL: http://0.0.0.0:8000/cart/cart.views.index
這似乎嘗試使用應該叫看法,作爲一個URL。
編輯:看來,當我加入一個隱藏字段的名稱屬性時出現錯誤:
<!-- Product/templates/product/product-template.html -->
<form action= {% url 'cart:index' %} method="post">
{% csrf_token %}
<!-- this link works -->
<a href={% url 'cart:index'%}> Click</a>
<!-- Submit button goes to correct URL when name attribute of hidden field below is commented out, but I need it to know what to put in cart -->
<input type="hidden" name="id" value="{{ article.id }}">
<input type="submit" value="Bestellen" class="btn btn-default"/>
</form>
當隱藏輸入字段的名稱屬性被刪除我收到以下錯誤:
MultiValueDictKeyError at /cart/
"'id'"
命名空間的URL應該工作,你可以嘗試創建一個簡單的鏈接索引頁(vs表單),檢查你的問題是否在其他地方? – zanderle
是的,我只是試過一個簡單的鏈接: Link 它的作品就像一個魅力。在添加此行時出現問題: input type =「hidden」name =「id」value =「{{article.id}}」>> 我需要知道要放入購物車的內容。更新後的問題 – Armannas
嗯,試着在'{%url'cart:index'%}'周圍放置''「'。所以這將意味着:'