1

I'm in the process of integrating Django_hosts. I have the front end URLs and the admin URLs successfully integrated and working, but my REST API URLs are still inaccessible due to this error:

'NoReverseMatch at /en/api/'

The error is traced to Django/urls/base.py, but I'm not sure what is triggering it:

Traceback (most recent call last):
  File "/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
  File "/lib/python3.6/site-packages/django/shortcuts.py", line 148, in resolve_url
    return reverse(to, args=args, kwargs=kwargs)
  File "/lib/python3.6/site-packages/django/urls/base.py", line 86, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'admin' is not a registered namespace

These are my hosts.py and api_urls.py files:

# hosts.py
host_patterns = patterns(
    '',
    host(r'www', settings.ROOT_URLCONF, name='www'),
    host(r'admin', 'Project.admin_urls', name='admin'),
    host(r'api', 'Project.api_urls', name='api'),
)

# api_urls.py
urlpatterns += i18n_patterns (
           path('session_security/', include('session_security.urls')),
           path('', include('Project.apps.api.urls'), name='api'),
           ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Also, to kill two birds with one stone, when attempting to convert this url:

{% url 'profile' request.user.id %}

to this url:

{% host_url 'profile' host 'www' request.user.id %}

It fails throwing a NoReverseMatch error with no arguments, as if the arguments are ignored.

Any help from someone knowledgeable about this package or either of these issues would be appreciated.

Thanks.

Update

I resolved this error by including admin.site urls within the api_urls.py file:

# api_urls.py
urlpatterns += i18n_patterns (
           path('session_security/', include('session_security.urls')),
           path('', include('Project.apps.api.urls'), name='api'),
           ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
           path('', admin.site.urls, name='admin'), <--This is key, but not sure exactly why.

However, now I've run into something strange, which isn't exactly an error, but effectively acts as one for my use case. The API seems to no longer recognize request.user, so that even if a user is logged in, the API thinks he is AnonymousUser.

*Both API and Admin subdomains not recognizing logged in user. Seems to be something to do with SESSION cookie...

0 Answers0