简体   繁体   中英

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. My main site URL patterns are:

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.

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:

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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