Python Django - 当前 URL 不匹配任何这些
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36743537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django - The current URL, , didn't match any of these
提问by Ravi Shanker Reddy
from django.conf.urls import include, url, patterns
from django.contrib import admin
#urlpatterns = [
# url(r'^mypage/', include('mypage.urls')),
# url(r'^admin/', admin.site.urls),
#]
urlpatterns = patterns('',
url(r'^$', 'mypage.views.home', name='home'),
url(r'^admin/', admin.site.urls),
)
The uncommented code is working fine. But as per tutorials the commented code also should have to work. But its showng an error of "The current url didnt match any of these". The code path is /ownblog/ownblog/urls.py
未注释的代码工作正常。但是根据教程,注释的代码也应该可以工作。但它显示了“当前网址与其中任何一个都不匹配”的错误。代码路径为/ownblog/ownblog/urls.py
urlpatterns = patterns('',
url(r'^$', 'views.home', name='home'),
)
The above code is in ownblog/mypage/urls.py
上面的代码在 ownblog/mypage/urls.py
def home(request):
return HttpResponse("Hello, world. You're at the polls index.")
The above code is in ownblog/mypage/views.py What I am missing
上面的代码在 ownblog/mypage/views.py 我所缺少的
回答by Alasdair
The error message when you visit http://localhost:8000/
is expected, because you haven't defined a url pattern for / in your commented code. The tutorial tells you to go to http://localhost:8000/polls/
. In your case, change that to http://localhost:8000/mypage/
because you use mypage
instead of polls
.
您访问时的错误消息http://localhost:8000/
是预期的,因为您尚未在注释代码中为 / 定义 url 模式。本教程告诉您转到http://localhost:8000/polls/
. 在您的情况下,将其更改为http://localhost:8000/mypage/
因为您使用mypage
而不是polls
.
The second error No module named views
is because you have used the string 'views.home'
in your url patterns instead of the callable views.home
. Make sure you include the import as well.
第二个错误No module named views
是因为您'views.home'
在 url 模式中使用了字符串而不是 callable views.home
。确保您也包括导入。
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
I notice that you are not following the 1.9 tutorialvery closely. For example you are using patterns()
and strings like 'mypage.views.home'
, which are both outdated since Django 1.8. I think you'd find it useful to follow the tutorial exactly before you begin changing lots of stuff.
我注意到您没有非常密切地关注1.9 教程。例如,您正在使用patterns()
和 之类的字符串'mypage.views.home'
,它们自 Django 1.8 以来都已过时。我认为您会发现在开始更改大量内容之前完全按照教程进行操作很有用。
回答by Colombian Developers
new features in django requires a new solutions...
django 中的新功能需要新的解决方案......
from django.conf.urls import url
from django.contrib import admin
from polls.views import urls
urlpatterns = [
...
url(r'^polls/$', urls),
]
type in your browser, tested in django 1.10
在浏览器中输入,在 django 1.10 中测试
Please download a working "HelloWorld" example in django 1.10
请在 django 1.10 中下载一个有效的“HelloWorld”示例
https://mega.nz/#!qFsgBbRD!lAKKprkMLmhJcaZ8h5NPxkZcfQpB8qrh1nU4rSSaRAo
https://mega.nz/#!qFsgBbRD!lAKKprkMLmhJcaZ8h5NPxkZcfQpB8qrh1nU4rSSaRAo
回答by Ritesh Sharma
You can just see the commented lines by django above your urlpatterns in the urls.py file.
您可以在 urls.py 文件中的 urlpatterns 上方看到 django 的注释行。
There will be a pattern of setting the path of the url you want to import, you can copy paste that and change the attributes according to the name of your views.
将有一个模式设置您要导入的 url 的路径,您可以复制粘贴并根据您的视图名称更改属性。
I saying this because the urlpatterns vary from version to version so to write url according to your django version you can refer to that.
我这么说是因为 urlpatterns 因版本而异,所以要根据您的 django 版本编写 url,您可以参考。
回答by Shreyas Jain
I think it is just the error with your syntax. Maybe you are following an old tutorial. Try this one:
我认为这只是你的语法错误。也许您正在学习旧教程。试试这个:
url(r'^$',views.home,name='home')
If this doesn't solve your problem, try this (not recommended personally; I still have some doubt in this region):
如果这不能解决您的问题,请尝试此操作(个人不推荐;我对该地区仍有一些疑问):
from django.urls import path
from mypage(appName) import views
...
urlpatterns = [
path(r'^$', views.home, name='home'),
path(r'^admin/', admin.site.urls),
]
This worked in my case once, but I'm still not sure why!
这曾经在我的情况下有效,但我仍然不确定为什么!