Fixing a NoReverseMatch in a Django Project for Registration
Hi I keep getting NoReverseMatch at /users/register/ however I am have followed the step by step for fixing this error:
Here is the urls.py
app_name = 'tac'
urlpatterns = [
path('terms-and-conditions/', TermsAndConditionsView.as_view(), namespace='terms_and_conditions'),
path('user-agreement/', UserAgreementView.as_view(), namespace='user_agreement'),
]
Here is the register.html that is causing the error:
<label class="form-check-label" for="terms_and_conditions">I agree to the <a href="{% url 'tac:terms_and_conditions' %}">terms and conditions</a>
</label>
Here is the traceback error:
Traceback (most recent call last):
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\Desktop\Project\users\views.py", line 34, in register
return render(request, 'users/register.html', {'form': form})
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\shortcuts.py", line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\template\backends\django.py", line 62, in render
return self.template.render(context)
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\template\base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\template\base.py", line 1005, in <listcomp>
return SafeString("".join([node.render_annotated(context) for node in self]))
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\template\base.py", line 966, in render_annotated
return self.render(context)
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\template\defaulttags.py", line 472, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\User\Desktop\Project\venv\lib\site-packages\django\urls\base.py", line 82, in reverse
raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'tac' is not a registered namespace
[06/Feb/2023 23:11:00] "GET /users/register/ HTTP/1.1" 500 126759
here is the main urls.py
path('tac/', include('tac.urls'), ),
it is also added in the settings INSTALLED_APPS
My question: How can I fix this error and why is it appearing?
Thanks
In main urls.py file, instead of
path('tac/', include('tac.urls'), ),
add :
path('tac/', include('tac.urls',namespace="tac"), ), #You can use any name instead of tac
Also, In app's urls.py file use name
instead of namespace
.
urlpatterns = [
path('terms-and-conditions/', TermsAndConditionsView.as_view(), name='terms_and_conditions'),
path('user-agreement/', UserAgreementView.as_view(), name='user_agreement'),
]
In your template, you can call it as :
{% url 'tac:terms_and_conditions' %}