Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Final Details url Tag

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

NoReverseMatch; Reverse for 'views.hello_world' not found. 'views.hello_world' is not a valid view function or pattern n

Reverse for 'views.hello_world' not found. 'views.hello_world' is not a valid view function or pattern name.

I'm getting the above error message after adding a link to the view views.hello_world. Below's my code. As far as I can tell, the code is exactly the same as in the video. Why does python not find the view?

layout.html:

{% load static from staticfiles %}

<!doctype html> <html> <head> <title>{% block title %}{% endblock %}</title> <link rel="stylesheet" href="{% static 'css/layout.css' %}"> </head> <body> <div class="site-container"> <nav> <a href="{% url "views.hello_world" %}">Home</a> </nav> {% block content %}{% endblock %} </div> </body> </html>

views.py:

from django.shortcuts import render from django.template.context_processors import request

def hello_world(request): return render(request, 'home.html')

urls.py:

"""Learning_site_6 URL Configuration

The urlpatterns list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import include, url from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from . import views

urlpatterns = [ url(r'^courses/', include('courses.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^$', views.hello_world), ]

urlpatterns += staticfiles_urlpatterns()

3 Answers

Bijaya Manandhar
Bijaya Manandhar
8,897 Points

After tons of research, I discovered the reason why the error "NoReverseMatch; Reverse for 'views.hello_world' not found." occurs. I believe it's totally because of the Django version that is installed. As a matter of fact, it would be great if the instructor could explain which version s/he is using while delivering videos. Yet the presentation is just great, thanks to Kenneth.

I have Django 2.2.7 installed. Now go ahead and give "views.hello_world" a name attribute equal to any name to replace the "views.hello_world" in the layout template. I have tried to make this clear as follows.

1) """learning_site/urls.py"""

urlpatterns = [
    url(r'^courses/', include('courses.urls')),
    url(r'^admin/', admin.site.urls), 
    url(r'^$', views.hello_world, name='hello'),
]

"""courses/urls.py"""

urlpatterns = [
  url(r'^$', views.course_list, name='list'),
  url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/$', views.step_detail, name='step'),
  url(r'(?P<pk>\d+)/$', views.course_detail, name='detail'),
]

2) Then, use the name "hello" in the "layout.html" instead of otherwise. The same method is true in the case of "courses.views.course_list" as well.

<nav>
    <a href="{% url 'hello' %}">Home</a>
    <a href="{% url 'list' %}">Courses</a>
</nav>

I hope it helps! PLEASE ADD COMMENT!

This is the answer that I was looking for, thank you!

Haydar Al-Rikabi
Haydar Al-Rikabi
5,971 Points

If your views.hello_world() is inside your app's directory, then you have to refer to it in your layout.html as:

<a href="{% url 'courses.views.hello_world' %}">Home</a> # views.hello_world is the wrong reference

Pay attention to where your views.hello_world() is located.

Seph Cordovano
Seph Cordovano
17,400 Points

If he's following the current tutorial then the views he referencing should be in the main app directory, not in the courses app.

I'm following the same course and I have the same issue, can you please share with us the way to handle it??