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 trialalex novickis
34,894 PointsDjango/python - render from template throws NoReverseMatch exception from statement {% url views.hello_world %}
I'm following along in the course but for curiosities sake I skipped over to the "final" version of the files as distributed and ease of discussion of this problem.
in home.html
{% extends "layout.html" %}
{% block title %}Well hello there!{% endblock %}
{% block content %}
<h1>Welcome to python h%&&! Prepare to die()</h1>
{% endblock %}
in 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>
<a href="{% url 'courses:list' %}">Courses</a>
</nav>
{% block content %}{% endblock %}
</div>
</body>
</html>
from django.shortcuts import render
import pdb
def hello_world(r):
pdb.set_trace()
return render(r, "home.html")
in urls,
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
#from . import views
from learning_site import views
from learning_site import urls
from learning_site.views import hello_world
urlpatterns = [
url(r'^$', views.hello_world )
]
urlpatterns += staticfiles_urlpatterns()
in views.py
from django.shortcuts import render
import pdb
def hello_world(request):
pdb.set_trace()
return render(request, "home.html" )
browsing to localhost:8000, I get a
NoReverseMatch at / Reverse for " with arguments '()' and keyword argument '{}' not found. 0 patterns tried
Looking at the stack trace it looks like it's failing on render in resolvers.py in _reverse_with_prefix()
(see http://dpaste.com/0S3XK24)
Hence after hitting the first breakpoint I added the next one with
from django.core.urlresolvers import reverse
from django.core.urlresolvers import RegexURLResolver
break RegexURLResolver._reverse_with_prefix
cont
In _reverse_with_prefix() they are doing something like
foo = self.reverse_dict.getlist(view)
however it returns an empty list
calling this manually after the breakpoint I see that I can try
foo = self.reverse_dict.getlist(view.hello_world)
which seems to do the same thing (IE empty set back)
on the other hand, if I first do a
from learning_app.views import views
while in pdb at the breakpoint
I see that it appears to return the correct list
Searching a bit on the web, I see that the suggested answer for this is to include a namespace in the original template
something like {%s url namespace:view %}
but what namespace ?? learning_app:view ? do I need to say {%s url 'learning_app:views.hello_world' %s}
I'm using python 3.5, with django -1.10a1-py3.5.egg in ubunto 16.04 latest which is in a vm (hence direct cut and paste are broken back to host o/s per vmware)
2 Answers
Myers Carpenter
6,421 PointsI didn't try to copy and paste your code and run it, but reading over it I would guess that
<a href="{% url 'courses:list' %}">Courses</a>
is causing the problem, because you don't have "courses:list" in the urls.py
file. I don't know why you aren't getting a more clear error message.
If you want to get more help I would suggest putting your code up on a github, so that others can clone your repo and run the code and see the same error you are getting.
alex novickis
34,894 PointsI solved it by changing the home view to have a name so instead of {% url views.hello_world %} I used {% url 'home' %}
working my way through the stack, It looks like the argument to url does not seem to make it all the way down when using the first form of the {% url object %} expression
the problem is present in the original .zip file for the class in "Final" so perhaps some semantics have changed in the version of Django I happened to use
Nursultan Bolatbayev
16,774 PointsNursultan Bolatbayev
16,774 PointsCould u use Markdown Cheatsheet as ur code is unreadable.