django_host raising 'accounts' is not a registered namespace error

271 Views Asked by At

I implemented django_hosts in my project and followed all the basics as documented here, everything seemed perfect until I started using the {% load hosts %} and {% host_url 'accounts:login' host 'accounts' %} template tags to reverse URLs just as it would normally be done with the default URL tag like {% urls 'accounts:login' %}. And now, I am getting the following errors.

NoReverseMatch at /en/auth/signup/personal/

'accounts' is not a registered namespace`

I have taken a closer look at my code again and again, but I can't find any issue with it. Has anyone experienced this kind of issue before?

sample HTML

{% extends 'base.html' %}
{% block content %}
{% load hosts %}
       <form class="form" method="post" enctype="multipart/form-data" role="form"
              action="{% host_url 'accounts:login' host 'accounts' %}">
            {% csrf_token %}
            {% bootstrap_messages %}
            {% bootstrap_form form show_label=False %}

            <button class="btn block-btn" type="submit" role="button">{% trans 'Sign in' %}</button>
        </form>
            <!--redirect-->
            <span class="text-center d-block">
                <a href="{% url 'password_reset' %}" class="form-footer-link centered-form-link">
                    {% trans "Can't login?" %}
                </a>

                <span class="middot">&bull;</span>

                <a href="{% host_url 'accounts:personal_signup' host 'accounts' %}"
                   class="form-footer-link centered-form-link">
                    {% trans "Sign up for an account" %}
                </a>
            </span>
{% endblock content %}

hosts.py

host_patterns = patterns('',
                         host(r'www', settings.ROOT_URLCONF, name='www'),
                         host(r'auth', 'accounts.urls', name='accounts'),
                         ...
)

my_site urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^i18n/', include('django.conf.urls.i18n')),
]

urlpatterns += i18n_patterns(
...
    path(_('auth/'), include('accounts.urls')),
...
)

my_app urls.py

app_name = 'accounts'
urlpatterns = [
    ...
    path(_('signup/personal/'), views.personal_account_signup, name='personal_signup'),
    ...    
]

settings.py

ROOT_HOSTCONF = 'mysite.hosts'
DEFAULT_HOST = 'www'
ROOT_URLCONF = 'mysite.urls'
`Traceback`

NoReverseMatch at /auth/login/
'accounts' is not a registered namespace
Request Method: GET
Request URL:    http://accounts.mysite.local:8000/auth/login/
Django Version: 3.1.7
Exception Type: NoReverseMatch
Exception Value:    
'accounts' is not a registered namespace
Exception Location: C:\Users\Dell\PycharmProjects\mysite\venv\lib\site-packages\django\urls\base.py, line 83, in reverse
Python Executable:  C:\Users\Dell\PycharmProjects\mysite\venv\Scripts\python.exe
Python Version: 3.9.1
Python Path:    
['C:\\Users\\Dell\\PycharmProjects\\mysite',
 'C:\\Users\\Dell\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\Dell\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\Dell\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\Dell\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\Dell\\PycharmProjects\\mysite\\venv',
 'C:\\Users\\Dell\\PycharmProjects\\mysite\\venv\\lib\\site-packages']
Server time:    Sun, 04 Apr 2021 10:41:30 +0100

Traceback Switch to copy-and-paste view
C:\Users\Dell\PycharmProjects\mysite\venv\lib\site-packages\django\urls\base.py, line 72, in reverse
                extra, resolver = resolver.namespace_dict[ns] …
▼ Local vars
Variable    Value
args    
[]
current_app 
'accounts'
current_ns  
'accounts'
current_path    
[]
kwargs  
{}
ns  
'accounts'
ns_converters   
{}
ns_pattern  
''
path    
['accounts']
prefix  
'/'
resolved_path   
[]
resolver    
<URLResolver 'accounts.urls' (None:None) '^/'>
urlconf 
'accounts.urls'
view    
'personal_signup'
viewname    
'accounts:personal_signup'
0

There are 0 best solutions below