我正在從事django電子商務項目。添加任何其他網址到我的html頁面single.html(顯示由特定的麪包師或製造商產品)給錯誤HTTP404。 所以在這個single.html頁面上顯示產品。我已添加到購物車按鈕。當我不提add_to_cart的按鈕標籤的鏈接它顯示的頁面,當我提到它給HTTP404錯誤Django error with get_absolute_url
views.py用於顯示製造商(在我的情況貝克)
def BakerDetail(request, slug):
try:
baker = Baker.objects.get(slug=slug)
products = Product.objects.filter(baker=baker)
context = {
'baker': baker,
'products': products
}
template = 'bakers/single.html'
return render(request, template, context)
except:
raise Http404
單對於add_to_cart
def Add_To_Cart(request, slug):
request.session.set_expiry(120000)
try:
the_id = request.session['cart_id']
except:
new_cart = Cart()
new_cart.save()
request.session['cart_id'] = new_cart.id
the_id = new_cart.id
cart = Cart.objects.get(id=the_id)
try:
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
pass
except:
pass
的.html
<table class='table'>
<thead>
<th></th>
<th>Products</th>
</thead>
<tbody>
{% for items in products %}
<tr>
<td></td>
<td>
<a href="{{ items }}">
{{ items }}</a>
{{ items.price }}
<button class="btn btn-primary" href="{% url 'add_to_cart' %}">Add To Cart</button>
</td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
視圖urls.py
url(r'^cart/(?P<slug>[\w-]+)/$', views.Add_To_Cart, name='add_to_cart'),
謝謝@Francois。有效。我會按照你所說的做出改變。 –