This is the most common error I get but the common reasons for getting this error only resolve it about half the time. In this case, I somehow broke all 22 tests for my home app without making any changes to the tests or the view.
#home.views.py
def index(request):
latest_blogs_list = Post.objects.filter(status=1).order_by('-created_on')[:3]
latest_projects_list = Projects.objects.order_by('-completion_date')[:3]
extended_projects_list = Projects.objects.order_by('-completion_date')[:10]
context = {
'latest_blogs_list': latest_blogs_list,
'latest_projects_list': latest_projects_list,
'extended_projects_list': extended_projects_list,
}
return TemplateResponse(request, "home/index.html", context)
#index.html (I reuse this in all my templates for all my apps)
<header>
{% include "home/mobile_nav.html" %}
{% include "home/desktop_nav.html" %}
{% include "home/hero.html" %}
</header>
{% include "home/footer.html" %}
#desktop_nav.html
<nav id='desktop' class='main-site'>
<ul>
<li><img src="" alt="logo"></li>
<li><a href="{% url 'home:home' %}">Home</a></li>
<li><a href="{% url 'home:about' %}">About</a></li>
<li><a href="">Store</a></li>
<li><a href="{% url 'portfolio:portfolio_home' %}">Portfolio</a> <i class="fas fa-sort-down"></i>
<ul class="dropdown">
{% if extended_projects_list %}
{% for project in extended_projects_list %}
<!--needs a dynamic link to the project-->
<li><a href="project_link">{{ project.name}}</a></li>
{% endfor %}
{% else %}
<li>No projects available.</li>
{% endif %}
</ul>
</li>
<li><a href="{% url 'blog:blog_home' %}">Blog</a></li>
<li><a href="{% url 'home:contact' %}">Contact</a></li>
<li><i class="fas fa-search"></i></li>
</ul>
</nav>
#mobile_nav.html
<nav id='mobile' class='main-site'>
<ul>
<li><img src="https://source.unsplash.com/30x30/?logo" alt="logo"></li> <!--logo placeholder-->
<li><i class="fas fa-bars"></i>
<ul class="dropdown">
<li><a href="{% url 'home:home' %}">Home</a></li>
<li><a href="{% url 'home:about' %}">About</a></li>
<li><a href="">Store</a></li>
<li><a href="{% url 'portfolio:portfolio_home' %}">Portfolio</a>
<ul class="dropdown port_drop">
{% if extended_projects_list %}
{% for project in extended_projects_list %}
<!--needs a dynamic link to the project-->
<li><a href="project_link">{{ project.title}}</a></li>
{% endfor %}
{% else %}
<li>No projects available.</li>
{% endif %}
</ul>
</li>
<li><a href="{% url 'blog:blog_home' %}">Blog</a></li>
<li><a href="{% url 'home:contact' %}">Contact</a></li>
</ul>
</li>
<li><i class="fas fa-search"></i></li>
</ul>
</nav>
#footer.html
<footer>
<div class="footer-desktop">
<h4>Recent Projects</h4>
{% if latest_projects_list %}
{% for project in latest_projects_list %}
<!--needs dynamic link to project -->
<p><a href="project_link">{{ project.title }}</a></p>
{% endfor %}
{% else %}
<p>No projects available.</p>
{% endif %}
<h4>Recent Blogs</h4>
{% if latest_blogs_list %}
{% for post in latest_blogs_list %}
<p><a href="">{{ post.title }}</a></p>
{% endfor %}
{% else %}
<p>No posts are available.</p>
{% endif %}
</div>
<div>
<h4>Follow Us</h4>
<i class="fab fa-facebook"></i>
<i class="fab fa-twitter-square"></i>
<i class="fab fa-instagram-square"></i>
<i class="fab fa-blogger"></i>
</div>
<nav class='footer-desktop'>
<ul>
<h4>Site Navigation</h4>
<li><a href="{% url 'home:about' %}">About</a></li>
<li><a href="">Store</a></li>
<li><a href="{% url 'blog:blog_home' %}">Blog</a></li>
<li><a href="{% url 'home:contact' %}">Contact</a></li>
</ul>
</nav>
<div class="footer-desktop">
<h4>Like what you see?</h4>
<p>If you're ready to make the future, then click this button and let's get started.</p>
<div class='button'><p>Click Here</p></div>
</div>
</footer>
As far as I know, the error began when I got rid of base.html and instead divided it into desktop_nav, mobile_nav, hero, and footer.html. The reason I did this was because django wanted to get the css links from base.html and I wanted to put the css links in the main templates. The tests all relate to listing blog posts and completed projects in the header and footer:
#this is just a sample, all of the tests give the same error
from .views import index
def test_latest_blogs_list_with_two_posts(self):
#creates a user and labels it as an author
User.objects.create(password="12345", is_superuser=False, is_staff=False, is_active=True,username="gemivere", first_name="Guinevere", last_name="Mayberry",
email="guineveremayberry@gmail.com", date_joined=datetime.now())
post_author = User.objects.get(username="gemivere")
#creates two blog posts
post_content = "This is a test. This is only a test. This test will tell us whether or not latest_blogs_list will display on the home page when there is only one post."
post1 = Post.objects.create(title="test1", slug="test1", author=post_author, content=post_content, status=1)
post2 = Post.objects.create(title="test2", slug="test2", author=post_author, content=post_content, status=1)
#tests
response = self.client.get(reverse(index))
self.assertEqual(response.status_code, 200)
self.assertContains(response, post1.title)
self.assertContains(response, post2.title)
self.assertQuerysetEqual(response.context['latest_blogs_list'], [post2, post1])
The error I get is “django.urls.exceptions.NoReverseMatch: Reverse for ‘home.views.index’ not found. ‘home.views.index’ is not a valid view function or pattern name.”