Django:带有动态 URL 的边栏:如何动态创建路径中包含动态文件夹的 URL
Django: sidebar with dynamic URLs: how to dynamically create URLs which have dynamic folders in the path
我在 Django 的边栏导航中遇到动态 URL 问题,我希望你们中的一些人能帮助我阐明如何解决它。我已经寻找过类似的问题,但找不到适合我的案例的答案。
基本上,我想要实现的是有一个带有 links 的侧边栏。此边栏将在许多页面上重复使用,因此它位于一个单独的 sidebar.py 文件中,稍后将导入到页面中。
<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>
我要显示的link如下:
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"),
如您所见,它们是采用 id 和 crawl_id 的动态 URL。因此,对于每个爬网仪表板,我希望侧边栏 link 到其相对 status_code_dashboard 页面和 url_depth_dashboard 页面。
举个例子:
/22/123/dashboard --> should have a sidebar with links to:
/22/123/dashboard/status-code/
/22/123/dashboard/url-depth/
我尝试做的是创建一个上下文处理器,如下所示:
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
}
...然后在 sidebar.py 模板中使用 {{depths_dashboard}}...
这有效,但它不可扩展:例如,当我在 /22/123/dashboard/status-code/ 时,我仍然希望 link 的边栏到其他部分。如果我使用上面的上下文处理器,由于错误的解决方案,错误的 links 将被创建为:
/22/123/dashboard/status-code/status-code/
/22/123/dashboard/status-code/url-depth/
关于如何使用基于 id 和 crawl_id 的动态 URL 在上述所有页面上显示边栏,您是否有提示?基本上问题是,我如何根据我所在的 id 和 crawl_id 上下文正确地动态发送这些参数?
非常感谢!
只需将 id
和 crawl_id
传递到您的模板中。然后在模板中:
<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>
如果你特别想使用预处理器,你也可以从get_full_path().split('/')
中得到这些数字。
我在 Django 的边栏导航中遇到动态 URL 问题,我希望你们中的一些人能帮助我阐明如何解决它。我已经寻找过类似的问题,但找不到适合我的案例的答案。
基本上,我想要实现的是有一个带有 links 的侧边栏。此边栏将在许多页面上重复使用,因此它位于一个单独的 sidebar.py 文件中,稍后将导入到页面中。
<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>
我要显示的link如下:
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"),
如您所见,它们是采用 id 和 crawl_id 的动态 URL。因此,对于每个爬网仪表板,我希望侧边栏 link 到其相对 status_code_dashboard 页面和 url_depth_dashboard 页面。
举个例子:
/22/123/dashboard --> should have a sidebar with links to:
/22/123/dashboard/status-code/
/22/123/dashboard/url-depth/
我尝试做的是创建一个上下文处理器,如下所示:
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
}
...然后在 sidebar.py 模板中使用 {{depths_dashboard}}...
这有效,但它不可扩展:例如,当我在 /22/123/dashboard/status-code/ 时,我仍然希望 link 的边栏到其他部分。如果我使用上面的上下文处理器,由于错误的解决方案,错误的 links 将被创建为:
/22/123/dashboard/status-code/status-code/
/22/123/dashboard/status-code/url-depth/
关于如何使用基于 id 和 crawl_id 的动态 URL 在上述所有页面上显示边栏,您是否有提示?基本上问题是,我如何根据我所在的 id 和 crawl_id 上下文正确地动态发送这些参数?
非常感谢!
只需将 id
和 crawl_id
传递到您的模板中。然后在模板中:
<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>
如果你特别想使用预处理器,你也可以从get_full_path().split('/')
中得到这些数字。