【问题标题】:Navigation Links in DjangoDjango 中的导航链接
【发布时间】:2020-08-10 04:57:33
【问题描述】:

如何在导航栏上设置正确的导航以返回主页或进入更多部分,例如“联系方式”或“价格”?我将展示我真正的意思的快速示例:

首页 -- 价格 -- 联系方式

在主页上,我按下“主页”按钮,当然它会刷新网站,因为我们在主页上。我按下价格按钮,它进入127.0.0.1:8000/prices,一切似乎都还好

  • 现在我们在价格部分页面 -

首页 -- 价格 -- 联系方式

但是现在当我按“联系”时它转到127.0.0.1:8000/prices/contact,但我希望它是127.0.0.1:8000/contact,或者现在按“主页”刷新站点,它不会返回主页。你能给我提示我现在该怎么办吗?

我的 urls.py(整个项目)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('main.urls') ),
]

我的 urls.py(应用程序)

urlpatterns = [
    path('', views.home, name="home-page"),
    path('prices/', views.prices, name="prices-page"),
    path('contact/',views.contact, name="contact-page"),
]

我的views.py(应用程序)

def home(request):
return render(request, 'home.html', {})
def prices(request):
return render(request, 'prices.html', {})
def contact(request):
return render(request, 'contact.html', {})

【问题讨论】:

  • 能否请您显示包含您的导航栏的 HTML 模板?听起来问题可能就在那里。

标签: python html django url


【解决方案1】:

再添加一条路径,比如 urlpatterns 中的home/..

urlpatterns = [
path('', views.home, name="home-page"),
path('home/', views.home, name="home-page"),
path('prices/', views.prices, name="prices-page"),
path('contact/',views.contact, name="contact-page"),

【讨论】:

    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多