Noreversematch error in Django

Asked

Viewed 793 times

0

Hello! Studying on Django and developing in this framework, I came across the following error:

Noreversematch at / Reverse for 'displayURL' with Arguments '('',)' not found. 1 Pattern(s) tried: ['profiles/(? P d+)$']

For better understanding of the situation, follow below my routes (py.), mine .html and my view functions (py views.)

index.html:

<!DOCTYPE html>
<html lang="pt-br">
  <head>
    <meta charset="utf-8">
    <title>ConnectedIn</title>
  </head>

  <body>
    <h1>Index</h1>
    {% if keyPerfil %}
      <ul>
        {% for perfil in keyPerfil %}
        <li>
          <a href="{% url 'exibirURL' keyPerfil.id %}">{{keyPerfil.nome}} / {{keyPerfil.email}}</a>
        </li>
        {% endfor %}
      </ul>
    {% else %}
      <p>Nenhum perfil encontrado</p>
    {% endif %}
  </body>

</html>

py views.:

from django.shortcuts import render
from perfis.models import Perfil

def index(request):
    return render(request, 'index.html', {'keyPerfil': Perfil.objects.all()})

def exibir(request, perfil_id):
    # necessario receber o parametro perfil_id, passado no urls.py

    perfil = Perfil.objects.get(id=perfil_id)

    return render(request, 'perfil.html', {'keyPerfil': perfil})

py.:

from django.urls import re_path
from perfis.views import index, exibir

urlpatterns = [
    re_path(r'^$', index, name='indexURL'),
    re_path(r'^perfis/(?P<perfil_id>\d+)$', exibir, name='exibirURL'),
]

1 answer

2


In your for in index.html you should call perfil.id and not keyPerfil.id

The line would look like this:

<a href="{% url 'exibirURL' perfil.id %}">{{perfil.nome}} / {{perfil.email}}</a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.