How to specify url link using name in a template ?
I would like to build main menu on my website and would like to use url names in template pages.
The problem is when I open the page in browser (http://example.com/test/), I’m getting NoReverseMatch
error.
Here is the code I have:
# INDEX.html
<a href="{% url 'index_page' %}">Homepage</a>
# urls.py in project folder
urlpatterns = [
path('test/', include('app_name.urls')),
# ...
]
# urls.py in app folder
urlpatterns = [
path('', views.index_page, name='index_page'),
# ...
]
# views.py
def index_page(request):
products = Product.objects.all()
context = {'products': products}
return render(request, 'app_name/INDEX.html', context)
Here is the error when I go to http://example.com/test/:
NoReverseMatch at /test/
Reverse for 'index_page' not found. 'index_page' is not a valid view function or pattern name.