Django: sidebar with dynamic URLs: how to dynamically create URLs which have dynamic folders in the path

Askew

I have a problem with dynamic URLs in sidebar navigation in Django and I hope some of you can help me shed some lights on how to solve it. I have looked for similar questions but I couldn't find an answer for my case.

Basically, what I want to achieve is to have a sidebar with links. This sidebar will be reused on many pages, so it sits in a separate sidebar.py file, which is later imported to the pages.

  <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
          <span>Content</span>
          <a class="d-flex align-items-center text-muted" href="#">
            <span data-feather="plus-circle"></span>
          </a>
        </h6>
        <ul class="nav flex-column">
          <li class="nav-item">
            <a class="nav-link active" href="DYNAMIC LINK HERE">
              <span data-feather="home"></span>
              Status codes</span>
            </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">
              <span data-feather="file"></span>
              Depth
            </a>
          </li>              
        </ul>

The links I want to display are the following:

urls.py

path('<id>/<crawl_id>/dashboard/', ProjectDashboard, name="crawl_dashboard"),
path('<id>/<crawl_id>/dashboard/status-codes/', StatusCodeDashboard, name="status_code_dashboard"),
path('<id>/<crawl_id>/dashboard/url-depth/', UrlDepthDashboard, name="url_depth_dashboard"),

As you can see, they are dynamic URLs which take an id and crawl_id. So, for each crawl dashboard I want the sidebar to link to its relative status_code_dashboard page and url_depth_dashboard page.

As an example:

/22/123/dashboard --> should have a sidebar with links to:
/22/123/dashboard/status-code/
/22/123/dashboard/url-depth/

What I tried to do is to create a context processor like the following:

def get_dashboard_paths(request):
    # Get current path
    current_path = request.get_full_path()

    depths_dashboard = current_path + 'url-depth/'

    return {
       'depths_dashboard': depths_dashboard
       
     }

...and then in the sidebar.py template use {{depths_dashboard}}...

This works but it's not scalable: when I am in /22/123/dashboard/status-code/ for example, I still want to have the sidebar to link to the other sections. If I use the above context processor, due to the bad solution wrong links would be created like:

/22/123/dashboard/status-code/status-code/
/22/123/dashboard/status-code/url-depth/

Do you have a hint on how I can display the sidebar on all of the above pages with dynamic URLs based on id and crawl_id? Basically the question is, how can I correctly send those parameters dynamically depending on which id and crawl_id context I am in?

Thanks a lot!

Sam Creamer

Just pass the id and crawl_id into your template. Then in the template:

<a href="/{{ id }}/{{ crawl_id }}/dashboard">Dashboard</a>
<a href="/{{ id }}/{{ crawl_id }}/dashboard/status-code">Status code</a>
<a href="/{{ id }}/{{ crawl_id }}/dashboard/url-depth">URL depth</a>

If you specifically want to use the preprocessor, you can also get these numbers from get_full_path().split('/').

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related