NoReverseMatch at /home/ u'home' is not a registered namespace
Question:
urls.py:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import patterns, include, url
from django.contrib import admin
from home import views
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.Home, name="home"),
url(r'^services/$', views.Services),
url(r'^contact/$', views.Contact)
)
But when I remove the ‘namespace=”home”‘ section, I get a ‘NoReverseMatch at /home/ u’home’ is not a registered namespace’. I’ve chanted the code around many times, but it’s always one of those two errors. The docs aren’t much help.
home.html:
{% url 'home' %}
{% load staticfiles %}
{% block doctype %}<!DOCTYPE HTML>{% endblock %}
{% load i18n %}
<html>
<head>
<title>{% block title %}{% endblock %}{% trans "name of website" %}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
{% block meta_tags %}{% endblock %}
<noscript>
<link rel="stylesheet" href="css/skel.css" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/style-wide.css" />
</noscript>
{% block stylesheet %}{% endblock %}
<script src="js/jquery.min.js"></script>
<script src="js/jquery.dropotron.min.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/skel-layers.min.js"></script>
<script src="js/init.js"></script>
{% block js %}{% endblock %}
</head>
<body class="{% block bodyclass %}{% endblock%}">
{% block page %}
<div id="header">{% block header_navigation %}
<h1><a href="home.html" id="logo">%{% trans "name of website" %}<em>description of stuff</em></a></h1>
<nav id="nav">
<ul>
<li class="current"><a href="{% url 'home:home.html' %}">Home</a></li>
<li><a href="{% url 'home:services.html' %}">Services</a></li>
<li><a href="{% url 'home:contact.html' %}">Contact Us</a></li>
</ul>
</nav>
{% endblock %}
</div>
Answers:
The parameter is called name
not namespace
:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.Home, name="home"),
url(r'^services/$', views.Services, name="services"),
url(r'^contact/$', views.Contact, name="contact"),
)
And then in the template:
<li class="current"><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'services' %}">Services</a></li>
<li><a href="{% url 'contact' %}">Contact Us</a></li>
namespace
is not a valid url
parameter, you need to use name
keyword.
url(r'^home/$', views.Home, name="home"),
And then you can use {% url 'home' %}
without problem.
check the url name you are giving in your html
(Home
) if it doesn’t match you will get "NoReverseMatch at /home/" error.
urls.py:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import patterns, include, url
from django.contrib import admin
from home import views
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.Home, name="home"),
url(r'^services/$', views.Services),
url(r'^contact/$', views.Contact)
)
But when I remove the ‘namespace=”home”‘ section, I get a ‘NoReverseMatch at /home/ u’home’ is not a registered namespace’. I’ve chanted the code around many times, but it’s always one of those two errors. The docs aren’t much help.
home.html:
{% url 'home' %}
{% load staticfiles %}
{% block doctype %}<!DOCTYPE HTML>{% endblock %}
{% load i18n %}
<html>
<head>
<title>{% block title %}{% endblock %}{% trans "name of website" %}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
{% block meta_tags %}{% endblock %}
<noscript>
<link rel="stylesheet" href="css/skel.css" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/style-wide.css" />
</noscript>
{% block stylesheet %}{% endblock %}
<script src="js/jquery.min.js"></script>
<script src="js/jquery.dropotron.min.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/skel-layers.min.js"></script>
<script src="js/init.js"></script>
{% block js %}{% endblock %}
</head>
<body class="{% block bodyclass %}{% endblock%}">
{% block page %}
<div id="header">{% block header_navigation %}
<h1><a href="home.html" id="logo">%{% trans "name of website" %}<em>description of stuff</em></a></h1>
<nav id="nav">
<ul>
<li class="current"><a href="{% url 'home:home.html' %}">Home</a></li>
<li><a href="{% url 'home:services.html' %}">Services</a></li>
<li><a href="{% url 'home:contact.html' %}">Contact Us</a></li>
</ul>
</nav>
{% endblock %}
</div>
The parameter is called name
not namespace
:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.Home, name="home"),
url(r'^services/$', views.Services, name="services"),
url(r'^contact/$', views.Contact, name="contact"),
)
And then in the template:
<li class="current"><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'services' %}">Services</a></li>
<li><a href="{% url 'contact' %}">Contact Us</a></li>
namespace
is not a valid url
parameter, you need to use name
keyword.
url(r'^home/$', views.Home, name="home"),
And then you can use {% url 'home' %}
without problem.
check the url name you are giving in your html
(Home
) if it doesn’t match you will get "NoReverseMatch at /home/" error.