NoReverseMatch Error in Django

0

Hello! Studying 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 a better understanding of the situation, follow my routes ( urls.py ) below, .html and my view functions ( views.py )

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>

views.py:

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})

urls.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'),
]
    
asked by anonymous 27.01.2018 / 00:45

1 answer

2

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

The line looks like this:

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