-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23e3f94
commit 360cb9c
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
id: 01t8dold57nwpw63e18ba80 | ||
title: Django | ||
desc: '' | ||
updated: 1687309242924 | ||
created: 1687307760199 | ||
--- | ||
|
||
# Highlight current page in navbar | ||
|
||
|
||
|
||
You can get the name of the url (referenced in your urlpatterns). Then set the 'active' class if the url matches. | ||
|
||
```python | ||
{% with url_name=request.resolver_match.url_name %} | ||
<li class="dropdown {% if url_name == 'index' %}active{% endif %}" | ||
<a href="{% url 'index' %}" >Home </a> | ||
</li> | ||
<li>...</li> | ||
{% endwith %} | ||
``` | ||
|
||
In practice it is also useful to prepend the app_name into the path, por example, there | ||
might be the `about:index` page and the `home:index` page, which both have the same | ||
`url_name` as of `Django 4.2`. In order to deal with this case, we also get the app name | ||
from the resolver, | ||
|
||
```python | ||
{% with app_name=request.resolver_match.app_names.0 url_name=request.resolver_match.url_name %} | ||
<li class="dropdown {% if app_name == 'home' and url_name == 'index' %}active{% endif %}" | ||
<a href="{% url 'home:index' %}" >Home </a> | ||
</li> | ||
<li>...</li> | ||
{% endwith %} | ||
``` | ||
|
||
|
||
# References | ||
|
||
1. [Django: Highlight current page in navbar](https://stackoverflow.com/questions/39639264/django-highlight-current-page-in-navbar) | ||
2. [get app name from url in django](https://stackoverflow.com/questions/19261269/get-app-name-from-url-in-django) |