3

I decided to have the front pages such as the main landing page and the 'about me' page etc. at the root of my project instead as a different app. This means the project looks like this:

/django-helloworld

  /Hello_World
    __init__.py
    url.py
    views.py
    wsgi.py

  /static
    style.css

  /templates
    index.html

My urls.py looks like this:

from django.conf.urls import url, include
from django.contrib import admin

from . import views

app_name = 'Hello_World'
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^admin/', admin.site.urls),

The problem is, when I try to point to a url in my template, it works by doing:

<a href="{% url 'index' %}">Home</a>

But if I try referencing the namespace like so:

<a href="{% url 'Hello_World:index' %}">Home</a>

I get this error:

NoReverseMatch at /
'Hello_World' is not a registered namespace

What am I doing wrong? Thanks in advance.

2
  • Is that your ROOT_URLCONF? Commented Oct 8, 2016 at 18:36
  • @knbk What exactly are you talking about? At settings.py I have ROOT_URLCONF = 'Hello_World.urls' Commented Oct 8, 2016 at 18:43

1 Answer 1

3

urls.py you are refering to is set as root url in your settings.py It probably looks like this

ROOT_URLCONF = 'Hello_World.urls'.

You cant namespace your root url because there can be only one root url.Namesapcing is done only when multiple app exists.

Instead you can mention the name of url and use it.

Ex: <a href="{% url 'index' %}">Home</a> 

The above will work in all of your templates and in all the apps WITHOUT namespacing because the href will first try for the urls.py file of your project where it will match the name index

url(r'^$', views.IndexView.as_view(), name='index'),.

The Reason django for django error saying namespace not matched beacuse it searches for other apps urls.py file for namespace and because it doesnt match app_name= 'Hello_World' else where the error is displayed.

Sign up to request clarification or add additional context in comments.

Comments

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.