NoReverseMatch at / with templates in different folders

Question:

I have a problem with Django Templates in nested folders.
I read the other questions with this problem, but I didn’t find the correct solution for my problem. Could you help me?

My project has the next schema:

 .
    ├── eventus
    │   ├── eventus
    │   │   ├── __init__.py
    │   │   ├── __pycache__
    │   │   │   ├── __init__.cpython-36.pyc
    │   │   │   ├── urls.cpython-36.pyc
    │   │   │   └── wsgi.cpython-36.pyc
    │   │   ├── db.sqlite3
    │   │   ├── settings
    │   │   │   ├── __init__.py
    │   │   │   ├── __pycache__
    │   │   │   │   ├── __init__.cpython-36.pyc
    │   │   │   │   ├── base.cpython-36.pyc
    │   │   │   │   └── local.cpython-36.pyc
    │   │   │   ├── base.py
    │   │   │   ├── local.py
    │   │   │   ├── prod.py
    │   │   │   └── staging.py
    │   │   ├── urls.py
    │   │   └── wsgi.py
    │   ├── manage.py
    │   └── myapps
    │       ├── __init__.py
    │       ├── __pycache__
    │       │   └── __init__.cpython-36.pyc
    │       ├── events
    │       │   ├── __init__.py
    │       │   ├── __pycache__
    │       │   │   ├── __init__.cpython-36.pyc
    │       │   │   ├── admin.cpython-36.pyc
    │       │   │   ├── models.cpython-36.pyc
    │       │   │   ├── urls.cpython-36.pyc
    │       │   │   └── views.cpython-36.pyc
    │       │   ├── admin.py
    │       │   ├── apps.py
    │       │   ├── migrations
    │       │   │   ├── 0001_initial.py
    │       │   │   ├── 0002_auto_20170924_2140.py
    │       │   │   ├── __init__.py
    │       │   │   └── __pycache__
    │       │   │       ├── 0001_initial.cpython-36.pyc
    │       │   │       ├── 0002_auto_20170924_2115.cpython-36.pyc
    │       │   │       ├── 0002_auto_20170924_2140.cpython-36.pyc
    │       │   │       └── __init__.cpython-36.pyc
    │       │   ├── models.py
    │       │   ├── templates
    │       │   │   ├── base.html
    │       │   │   └── events
    │       │   │       ├── base_events.html
    │       │   │       └── index.html
    │       │   ├── tests.py
    │       │   ├── urls.py
    │       │   └── views.py
    │       └── users
    │           ├── __init__.py
    │           ├── __pycache__
    │           │   ├── __init__.cpython-36.pyc
    │           │   ├── admin.cpython-36.pyc
    │           │   └── models.cpython-36.pyc
    │           ├── admin.py
    │           ├── apps.py
    │           ├── migrations
    │           │   ├── 0001_initial.py
    │           │   ├── __init__.py
    │           │   └── __pycache__
    │           │       ├── 0001_initial.cpython-36.pyc
    │           │       └── __init__.cpython-36.pyc
    │           ├── models.py
    │           ├── tests.py
    │           └── views.py
    └── requirements
        ├── base.txt
        ├── local.txt
        ├── prod.txt
        └── staging.txt

And my templates/base.html is :

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock title %}</title>
</head>
<body>
    {% block content %}

    {% endblock content %}
</body>
</html>

and my templates/events/base.html is

{% extends "base.html" %}

and mi templates/events/index.html is

 {% extends "events/base_events.html" %}

{% block title %}Home{% endblock title %}

{% block content %}

<div class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Eventus</a>
        </div>
        <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav pull-right">
                {% if user.is_authenticated %}
                    <li><a href="{% url 'events_app:panel' %}">Hola {{ user.username|capfirst }}</a></li>
                {% else %}
                <li><a href="{% url 'events_app:panel' %}">Sign Up / Login</a></li>
                {% endif %}
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</div>

<div class="jumbotron">
  <div class="container text-center">
    <h1>EVENTUS</h1>
    <p>Bienvenido a EVENTUS, aquí podrás encontrar eventos que se den en tu ciudad e inscribirte on-line con un solo click.</p>
    <p>¡<a href="{% url 'users_app:login' %}" role="button">Registrate</a> y crea tu evento ahora!</p>
  </div>
</div>

<div class="container">

    <div class="page-header">
        <h4><strong>Eventos disponibles</strong></h4>
    </div>

    <div class="row">

        <div class="col-md-2">
            <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-tag"></span></span>
                <select class="form-control">
                    <option>Categoría</option>
                </select>
            </div>
        </div>
        <div class="col-md-2">
            <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-credit-card"></span></span>
            <select class="form-control">
                <option>Tipo pago</option>
            </select>
            </div>
        </div>
        <div class="col-md-8">
            <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span>
                <input type="text" class="form-control" placeholder="Busca el evento de tu gusto">
            </div>
        </div>

    </div>

</div>

<p></p>

<div class="container eventos">    

    <div class="row contenedor-eventos">
        {% for event in events %}
        <div class="col-sm-1 col-md-3 ">
            <div class="thumbnail equal">
                <img src="{{ event.imagen.url }}" alt="250x180">
                <div class="caption">
                    <h4><a href="">{{ event.name }}</a></h4>
                    <small class="date">{{ event.start }}</small>
                    <div class="place">
                        <small class="place">{{ event.place}}</small> / 
                    </div>
                    <span class="views">{{ event.views }}</span>
                </div>
            </div>
        </div>
        {% endfor %}

    </div>

</div>

<div class="container categorias">    

    <div class="page-header">
        <h4><strong>Categorías relacionadas</strong></h4>
    </div>

    <div class="row">
        <div class="col-sm-1 col-md-12">
        {% for category in categories %}
            <a class="btn btn-default btn-lg btn-fix">
                <span class="glyphicon glyphicon-tag"></span> {{ category.name }}
            </a>
        {% endfor %}
        </div>

    </div>

</div>

<div class="container organizadores">    

    <div class="page-header">
        <h4><strong>Organizadores destacados</strong></h4>
    </div>

    <div class="row">

        <div class="col-sm-1 col-md-2">
            <div class="thumbnail">
                <img src="http://placehold.it/300x180" alt="250x180">
                <div class="caption">
                    Nombre organizador
                </div>
            </div>
        </div>

    </div>

</div>


{% endblock content %}

my view.py is

from django.shortcuts import render
from .models import Event, Category
# Create your views here.
def index(request):
    events = Event.objects.all().order_by('-created')[:6]
    categories = Category.objects.all()
    return render(request, 'events/index.html', {'events': events, 'categories': categories})

my url.py is

from django.conf.urls import url, include

urlpatterns = [
    url(r'^$', include('myapp.events.views.index'),
    url(r'^admin/', admin.site.urls),
]

and my base.py in templates I have this configuration

import os
    from unipath import Path

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = Path(__file__).ancestor(3)


    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = '70r=ho4co205hed75q!)am+dnqi=lj(98$lnadxmgf2n(s&_qi'

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    ALLOWED_HOSTS = []


    # Application definition
    DJANGO_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]

    LOCAL_APPS = [
        'myapps.events',
        'myapps.users',
    ]

    THIRD_PARTY_APPS = [
    ]

    INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS + THIRD_PARTY_APPS

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]

    ROOT_URLCONF = 'eventus.urls'

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]

    WSGI_APPLICATION = 'eventus.wsgi.application'

    # Password validation
    # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

    AUTH_PASSWORD_VALIDATORS = [
        {
            'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        },
        {
            'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
        },
    ]


    # Internationalization
    # https://docs.djangoproject.com/en/1.11/topics/i18n/

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'UTC'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True


    AUTH_USER_MODEL = 'users.User'

And the error of my page is the next:

NoReverseMatch at /
'events_app' is not a registered namespace
Request Method: GET
Request URL:    http://localhost:8000/
Django Version: 1.11.5
Exception Type: NoReverseMatch
Exception Value:    
'events_app' is not a registered namespace
Exception Location: /Users/dmuino/.virtualenvs/Pruebas/lib/python3.6/site-packages/django/urls/base.py in reverse, line 87
Python Executable:  /Users/dmuino/.virtualenvs/Pruebas/bin/python
Python Version: 3.6.1
Python Path:    
['/Users/dmuino/.virtualenvs/Pruebas/CursoProDjango/eventus',
 '/Users/dmuino/.virtualenvs/Pruebas/lib/python36.zip',
 '/Users/dmuino/.virtualenvs/Pruebas/lib/python3.6',
 '/Users/dmuino/.virtualenvs/Pruebas/lib/python3.6/lib-dynload',
 '/Users/dmuino/anaconda/lib/python3.6',
 '/Users/dmuino/.virtualenvs/Pruebas/lib/python3.6/site-packages']
Server time:    Mon, 25 Sep 2017 12:28:14 +0000
Error during template rendering

In template /Users/dmuino/.virtualenvs/Pruebas/CursoProDjango/eventus/myapps/events/templates/base.html, error at line 0
'events_app' is not a registered namespace
1   <!DOCTYPE html>
2   <html lang="en">
3   <head>
4       <meta charset="UTF-8">
5       <title>{% block title %}{% endblock title %}</title>
6   </head>
7   <body>
8       {% block content %}
9           
10      {% endblock content %}
Asked By: Diego muino

||

Answers:

You must pass a namespace when using django.conf.urls.include()

url(r'^$', include('myapp.events.views.index', namespace='events_app'))

Alternatively, declare a app_name string in the the file that is included. Here’s an example from the django docs:

# polls/urls.py
from django.conf.urls import url

from . import views

app_name = 'polls'  
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>d+)/$', views.DetailView.as_view(), name='detail'),
    ...
]

Instead of polls, you can use app_name = 'events_app'

Answered By: Håken Lid

The url.py that I put in eventus/url.py is the next:

"""eventus 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 url, include
from django.contrib import admin
from myapps.events import views
urlpatterns = [
    url(r'^', views.index),
    url(r'^admin/', admin.site.urls),
]
Answered By: Diego muino

I find the problem, when I copy the index.html, we copy the next:
events_app:panel and users_app:login but this part is no defined.

thanks to all for your help.

Answered By: Diego muino
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.