简体   繁体   English

如何在 Django 中为同一个应用程序创建两个 url?

[英]How to make two urls to the same app in Django?

It's my main urls.py :这是我的主要urls.py

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

And it's my urls.py of 'product' app:这是我的“产品”应用程序的urls.py

urlpatterns = [
    path('<slug:slug>/', views.getProductPage),
    path('category/<slug:slug>/', views.getCategoryProducts),
]

I would like to have such links:我想要这样的链接:

localhost/category/smartphones - shows all smartphones localhost/category/smartphones - 显示所有智能手机

localhost/xiaomi-redmi-note-7 - shows a page of xiaomi redmi note 7 smartphone localhost/xiaomi-redmi-note-7 - 显示小米红米 Note 7 智能手机的页面

The problem is that '' directs to 'account' app and i'll not able to create localhost/slug:slug link问题是“”指向“帐户”应用程序,我将无法创建 localhost/slug:slug 链接

Try to include your product urls above the account urls.尝试将您的产品网址包含在帐户网址上方。 But I think sometimes you may get overlapping url.但我认为有时您可能会得到重叠的 url。 Please read this Precedence of Django url .请阅读这个Django url 的优先级 Hope my answer help you.希望我的回答对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM