Brand new to Django, and running through the beginner tutorial, I made some adjustments to model an e-commerce site I hope to develop using Django. I’ve successfully set up an index view/url/template to display a list of all available products as links to the individual products, but clicking the link returns a debug 404 page.
The bad link is generated in my index template as (some HTML committed):
{% if products_list %}
unordered list
{% for product in products_list %}
a href="/detail/{{ product.id }}/">{{ product.name }}
{% endfor %}
/unordered list
{% else %}
No products are available.
{% endif %}
And now my detail code, I am attempting to think this view to display unique info depending upon which product link is clicked:
VIEW:
def detail(request, product_id):
try:
product = Product.objects.get(pk=product_id)
except Product.DoesNotExist:
raise Http404(“Product does not exist”)
return render(request, ‘webCart/detail.html’, {‘product’: product})
URL PATH:
path(‘detail/int:product_id/’, views.detail, name=‘detail’)
DETAIL TEMPLATE (some HTML committed):
h1-product name
unordered list
{% for description in product.description_set.all %}
{{ product.description }}
{% endfor %}
/unordered list
Right now clicking a product on the index page returns the URL:
127.0.0.1:8000/product/4/ where four is the product id I clicked on
Again I am very new to Django and thank anyone/everyone who takes the time to help with what is no doubt a very clumsy beginner’s mistake.