简体   繁体   English

Django Home url 设置为文件夹

[英]Django Home url set to folder

I have an issue where I would like my address (whether local or live) to point to my projects app as my home page, yet every change I make breaks the other URLs.我有一个问题,我希望我的地址(无论是本地的还是现场的)指向我的项目应用程序作为我的主页,但我所做的每一次更改都会破坏其他 URL。 My main site URL patterns are:我的主要网站 URL 模式是:

urlpatterns = [
    path('admin/', admin.site.urls),
    path("projects/", include("projects.urls")),
    path("blog/", include("blog.urls")),
]

I have tried to change the path("project/", include("projects.urls")) to path("", include("projects.urls")) which breaks the blog index.我试图将path("project/", include("projects.urls"))更改为path("", include("projects.urls")) ,这会破坏博客索引。

my blog has the following pattern:我的博客有以下模式:

urlpatterns = [
    path("", views.blog_index, name="blog_index"),
    path("<slug:slug>/", views.blog_detail, name="blog_detail"),
    path("<category>/", views.blog_category, name="blog_category"),
]

And my projects:还有我的项目:

urlpatterns = [
    path("", views.project_index, name="project_index"),
    path("<slug:slug>/", views.project_detail, name="project_detail"),
    path("project/<tag>/", views.project_tag, name="project_tag"),
]

The conflict is coming from django not knowing the difference between:冲突来自 django,不知道两者之间的区别:

http://example.com/blog

and

http://example.com/(project_slug_called_blog)

because they look functionally identical, just being a string.因为它们看起来功能相同,只是一个字符串。

You could reverse the order of the URLs so that blog is looked for first您可以颠倒 URL 的顺序,以便首先查找博客

urlpatterns = [
    path('admin/', admin.site.urls),
    path("blog/", include("blog.urls")),
    path("", include("projects.urls")),
]

And that should work so long as not projects are called 'blog'只要项目不被称为“博客”,这应该可以工作

Or, failing that, you can make the project detail view's URL more explicit或者,如果做不到这一点,您可以使项目详细视图的 URL 更加明确

path("<slug:slug>/view-details", views.project_detail, name="project_detail"),

I'd personally go with the second option as it makes the URL more human readable and is likely a more robust solution.我个人会选择第二个选项,因为它使 URL 更易于阅读,并且可能是一个更强大的解决方案。

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

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