Django problem, when I pass product id by get method

In href tag I want to send product id but this code showing an error.

Could not parse the remainder: 'data.id' from ''viewproduct'data.id'

{% for data in data %}

                    <div class="item mt-5">

                        <div class="card" >

                        **<a href={% url 'viewproduct'data.id %}>**
                         
                            <img class="women" src="{{data.image.url}} "alt="First slide">
                        </div>
                        <div class="card-body text-center">
                            <h4>{{data.name}}</h4>  

                        </div></a>
                    </div>

                
               {% endfor %}

There's a mismatch between your named url in urls.py and the one used in your templates. From your comments, the name of your product_detail url is product_detail but you are trying to use the reverse for viewproduct in your templates. So it should actually be:

<a href={% url 'product_detail' data.id %}>

Since we didn't see your views, this is also assuming you have a context data in your views.py with an attribute id. Something like this:

from django.shortcuts import render, get_object_or_404
from .models import Product

def product_detail(request, product_id):
    data = get_object_or_404(Product, id=product_id)
    context = {'data': data}
    return render(request, 'product_detail.html', context) 

Also note that you need to leave a space between the named url and the id. What you sent is joined together and Django would not be able to recognize the remainder.

You need to add a space between the string literal 'viewproduct' and data.id, so {% url 'viewproduct' data.id %}.

Вернуться на верх