首页 > 解决方案 > Plotly Dash 与 Django 集成:在侧边栏应用导航时,当前路径welcome.html 与其中任何一个都不匹配

问题描述

我已遵循此视频教程,并成功应用了所有步骤并执行了。它工作正常。我现在正在尝试在侧边栏上添加导航,例如此侧边栏导航菜单底部的表格:但它给出了以下错误:在此处输入图像描述

我的主urls.py如下:

from django.contrib import admin
from django.urls import path, include    
urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('home.urls')),
        path('django_plotly_dash/', include('django_plotly_dash.urls')),
    ] 

我的应用程序urls.py是这样的:

from django import urls
from django.urls import path
from . import views
from home.dash_apps.finished_apps import simpleexample

urlpatterns= [

    path('', views.home, name='home'),
    path('tables/', views.home1, name='home1')
    
]

而我的sidebar.html如下:

<li class="nav-item">
        <a class="nav-link" href="{% url 'home1' %}">
            <i class="fas fa-fw fa-table"></i>
            <span>Tables</span></a>
    </li>

此外,我在views.py中呈现这个

def home1(request):
    def scatter():
        x1 = ['PM2.5','PM10','CO2','NH3']
        y1 = [30, 35, 25, 45]

        trace = go.Scatter(
            x=x1,
            y = y1
        )
        layout = dict(
            title='Line Chart: Air Quality Parameters',
            xaxis=dict(range=[min(x1), max(x1)]),
            yaxis = dict(range=[min(y1), max(y1)])
        )

        fig = go.Figure(data=[trace], layout=layout)
        plot_div = plot(fig, output_type='div', include_plotlyjs=False)
        return plot_div

    context ={
        'plot1': scatter()
    }

    return render(request, 'home/welcome.html', context)

我无法理解如何正确定位welcome.html,以便在我单击仪表板侧栏中的表格时加载它。

标签: pythondjangoplotly-dash

解决方案


url 错误,需要按照 URL 定义的 url:

path('', views.home, name='home'),
path('tables/', views.home1, name='home1')

尝试:

172.20.43.46.8001/tables/

推荐阅读