Django'/'仅首页网址错误 [英] Django '/' only homepage url error

查看:90
本文介绍了Django'/'仅首页网址错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Django 2.0,现在我不知道如何为首页创建一个空网址。意思是,我希望它路由到 web.com / web.com 。我尝试了这段代码,但是它不起作用:

I am using Django 2.0 and now I have no idea how to make an 'empty' url for the homepage. Meaning, I want it to route for web.com/ or web.com. I tried this code but it does not work:

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

...和 post.urls

urlpatterns = [
    path('', views.index, name='index')
]

当我向 localhost:8000 发出请求时得到的错误:

And the error I get when I make a request to localhost:8000:


请求URL: http:// localhost:8000 /
使用URLconf Django在myblog.urls中定义,按以下顺序尝试了这些URL模式:

Request URL: http://localhost:8000/ Using the URLconf defined in myblog.urls, Django tried these URL patterns, in this order:


  1. admin /

  1. admin/

/

空路径与这些都不匹配。

The empty path didn't match any of these.

我确实通过将 path 设置为空字符串找到了一种解决方法都'',但是我不确定是否推荐它可能导致什么错误。非常感谢您的帮助。谢谢:-)。

I did sort of find a workaround by setting path to an empty string '' on both but I am not sure if it is recommended or what errors it might cause. Help is much appreciated. Thank you :-).

推荐答案

您不需要 /作为Home url,只需将两个路径都保留为 。
主网址127.0.0.1:8000/与127.0.0.1:8000相同。
此URL模式适用于主页。

You don't need "/" for Home url, just leave both the path with "". The home url 127.0.0.1:8000/ is nevertheless is same as 127.0.0.1:8000. This URL pattern will work for the home page.

urls.py

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

posts.urls

urlpatterns = [
    path('', views.index, name='index')
]

这篇关于Django'/'仅首页网址错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆