2

I tried to link an anchor to another page in Django. But I get the error " Reverse for 'animals.all_animals' not found. 'animals.all_animals' is not a valid view function or pattern name." I tried several ways to do it.. no success. I have one app called animals and Im tyring to display the list of animals in the by clicking an anchor on the homepage. I attached here my Django files.

from django.shortcuts import render, get_object_or_404

from .models import Animal


def animal_list(request):
    animals = Animal.objects.all()
    return render(request, 'animals/animal_list.html', {'animals': animals})

// and here is the html

{% for animal in animals %}
<h1>{{animal.species}}</h1>
<p>{{animal.description}}</p>
{% endfor %}
from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.animal_list, name='all_animals'),
    url(r'^(?P<pk>\d+)/$', views.animal_detail, name='all_details'),
]
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Animals Site</title>
    <link href="{% static 'css/base.css'%}" rel="stylesheet">
</head>
<body>

{% block content %}
<nav>
    <a href="{% url 'animals.all_animals'%}">Animal List</a>
</nav>
  <a></a><h2>I love cats!</h2>
{% endblock content %}
{% block listbar %}
  <ul>
      <li>Sphynx</li>
      <li>Catto</li>
      <li>Bengal</li>
  </ul>
{% endblock listbar %}
</body>
</html>
{% block listcolor%}
<style>
    h2{
     font-family: 'Calibri';
     color: blue;
    }
</style>
{% endblock listcolor%

1 Answer 1

2

You need a colon not a dot in the notation:

<a href="{% url 'animals:all_animals' %}">Animal List</a>

Or in case the included urls from your app are not namespaced:

<a href="{% url 'all_animals' %}">Animal List</a>
Sign up to request clarification or add additional context in comments.

5 Comments

'animals' is not a registered namespace
Then "{% url 'all_animals' %}" should do. You didn't name the included urls from the app.
Yes, It worked like that. But for example, If I want to access: " url(r'^(?P<pk>\d+)/$', views.animal_detail, name='all_details')," it doesnt work. It says 1 pattern(s) tried: ['animals/(?P<pk>\\d+)/$']
That's a new problem with a different view altogether.
Consider asking this as a new question. Code formatting in the comments is not as good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.