我正在使用 Django 2.0,现在我不知道如何为主页制作一个“空”网址。意思是,我希望它路由到 web.com/
或 web.com
。我试过这段代码,但它不起作用:
urlpatterns = [
path('admin/', admin.site.urls),
path('/', include('post.urls'))
]
...和post.urls
urlpatterns = [
path('', views.index, name='index')
]
当我向 localhost:8000
发出请求时出现的错误:
Request URL: http://localhost:8000/ Using the URLconf defined in myblog.urls, Django tried these URL patterns, in this order:
admin/
/
The empty path didn't match any of these.
我确实通过在两者上将 path
设置为空字符串 ''
找到了一种解决方法,但我不确定是否推荐这样做或可能导致什么错误.非常感谢您的帮助。谢谢 :-)。
最佳答案
Home url 不需要“/”,只需将两个路径都保留为“”。 主页 url 127.0.0.1:8000/与 127.0.0.1:8000 相同。 此 URL 模式适用于主页。
网址.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('post.urls'))
]
和posts.urls
urlpatterns = [
path('', views.index, name='index')
]
关于python - Django '/' 只有主页 url 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48064071/