what are the conditions in which we get NoReverseMatch error

what are the conditions in which we get NoReverseMatch error:

i get this error many times in my project. I want to know why and how this error is raised and how to solve this problems.

1 Like

It’s caused by trying to reverse a url name that isn’t defined. It’s typically thrown by a call to reverse or reverse_lazy. It’s resolved by ensuring all calls to reverse reference a name defined for a url.

Sir, for example if I have

{% include ‘homemain.html’%}
{% block content %}
{%for profil in profilki%}

<h3>  
<a href="{%url 'profilepage' profilepage.id%}"> {{profil}} - profiles  </a>
</h3>
{% endfor %} {% endblock content %}

urls.py
path (‘profilepage/str:pk/’,views.profilepage,name=“profilepage”),
path (‘profilki/’, views.profilki,name=“profilki”),

and passing http://127.0.0.1:8000/profilepage/1/ returns me the page Im trying to access, where is the issue ?

You have:

This will create an error if any of the following are true:

  • You do not have an object named profilepage in your context.
  • The profilepage object does not have an attribute named id
  • The profilepage.id entity does not provide a value.
1 Like
def profilki(request):
     
  profilki = profile.objects.all()
     
     context = {'profilki':profilki }
 
     return render(request,'base/profilki.html',context)
 
 
 
 def profilepage(request,pk):
     user = Trainingvalue.objects.get(id=pk)
     trainingplan = Trainingvalue.objects.all()
     profilepage= profile.objects.get(id=pk)
     user_training= Trainingvalue.objects.filter(user=profilepage.user)
     context ={'user_training':user_training,'user':user,'profilepage':profilepage,
     'trainingplan':trainingplan,}
 
     return render(request, 'base/profilepage.html' , context)

url : path ('profilepage/<str:pk>/',views.profilepage, name="profilepage")

profilepage in context V
profile page has an attribute id V
profilepage.id entity does not provide value - yet i can access it through the borwser…

so technically this should work:

<a href=" {%url 'profilepage' profilepage.id %} "> profile </a>

Side note: When posting code here, please enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

Please edit your post. Also clarify which of the two views posted is causing the error to be thrown and which template file that <a .. tag is in.

1 Like

Sir I’ve resolved issue by putting it this way in a template :


 
 {% include 'homemain.html'%}
 
 {% block content %}
 
 {%for profil in profilki%}
 
 <div>
   
 <h3>{{profil}} -<a href="{%url 'profilepage' profil.id %}">profile </a></h3>
 
     
 </div>
 
 {% endfor %}
 
{% endblock content %}

Thank you for your help, it made me think