When an item is clicked on, it should take the user to a page where there is more information about the item. However, when I click on an item, I get the error: NoReverseMatch: Reverse for 'listing' with arguments '('', '')' not found. 1 pattern(s) tried: ['(?P<id>[0-9] )/(?P<name>[^/] )/\\Z']
.
This line in my HTML template is highlighted on the error page: <a href="{% url 'listing' i.pk i.name %}">
but I do not know why. The i.pk
and i.name
are arguments for a views.py
function that is called listing
that returns a page (listing.html
) that has more info about the item. The url should be in the format http://127.0.0.1:8000/ID/NAME_OF_ITEM
where ID
is the actual id of the item and the NAME_OF_ITEM
is the actual name of the item.
How do I fix this?
index.html: (this page works fine)
{% for i in listings %}
<div >
<div >
<div >
<div >
<a href="{% url 'listing' i.pk i.name %}">
<h4>{{ i.name }}</h4>
</a>
</div>
<div >
<a href="{% url 'listing' i.pk i.name %}">
<strong>£{{ i.highest_bid }}</strong>
</a>
</div>
</div>
<div >
<a href="{% url 'listing' i.pk i.name %}">
<p>{{ i.description }}</p>
</a>
</div>
</div>
</div>
{% endfor %}
(the rest is for the listing.html
page) -->
views.py:
def listing(request, id, name):
listing = Listing.objects.get(pk=id)
return render(request, 'auctions/listing.html', {
'listings': Listing.objects.all(),
'listing': listing
})
urls.py:
path('<int:id>/<str:name>/', views.listing, name='listing')
listing.html: (this is where the error occurs)
<main>
<div >
<div >
<h2> {{ listing.name }} </h2>
</div>
</div>
<div >
<div >
<h2> {{ listing.name }} </h2>
</div>
<div >
{% if listing.photo_present != None %}
<div >
{% if listing.photo_url != None %}
<div >
<a href="{% url 'listing' i.id i.name %}">
<img src=" {{ i.photo_url }} " alt="">
</a>
</div>
{% else %}
<div >
<a href="{% url 'listing' i.id i.name %}">
<img src=" media/{{ i.photo_upload }} " alt="">
</a>
</div>
{% endif %}
{% endif %}
</div>
</div>
</main>
CodePudding user response:
{% url 'listing' i.id i.name %}
In the listing.html page template, there is no i
variable, so i.id
and i.name
resolve to blank strings. But the url definition for listing
requires that those values be nonblank, so you get an error.
In the index.html page, those variable references are contained in a {% for i in listings %}
loop, so the i
variable properly exists on that page.
But that is not the case in the listing.html page.
Frankly, I wonder why that page tries to show that link at all, because it would just be a link to the page you're already on.
It looks like you created the listing.html page template by copying the index.html page, and just forgot to change some things.
CodePudding user response:
I copy and pasted the link to the image from the index.html
page, where there is a loop and i
is the current listing in the loop. However, on the listing page there is no loop so there is no i
as it focuses on only one listing.
So the anything that contains i.SOMETHING
is wrong as the computer does not recognize i
as there is none. The images are wrapped in links that link to the current page: {{ url 'listing' i.id i.name }}
and this is what is causing the error.
I removed all of the times that this came up because I no longer need the images to link to the listing page as the user should currently be on the listing page itself.