Python 当前 URL app/ 与这些都不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34220959/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:38:19  来源:igfitidea点击:

The current URL, app/, didn't match any of these

pythondjangopython-3.xdjango-urlsdjango-1.10

提问by Dhaval

I'm a newbie in Django and just started looking at it before a day by installing Django 1.10 on my local.

我是 Django 的新手,并在一天前通过在本地安装 Django 1.10 开始查看它。

I've followed all the instructions of this link https://docs.djangoproject.com/en/dev/intro/tutorial01/. However I'm continuously getting this error:

我已按照此链接https://docs.djangoproject.com/en/dev/intro/tutorial01/ 的所有说明进行操作。但是我不断收到此错误:

Page not found (404) Request Method:    GET Request URL:    http://127.0.0.1:8000/polls/

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

    ^admin/

The current URL, polls/, didn't match any of these.

I've created polls app to getting started.

我已经创建了民意调查应用程序以开始使用。

To make a start, I went with view.py, here is the code:

首先,我使用 view.py,这是代码:

polls/views.py

民意调查/views.py

from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello World!")

Here is my code for urls.py:

这是我的 urls.py 代码:

polls/urls.py

民意调查/网址.py

from django.conf.urls import patterns, url

from . import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
)

And here is the urls.py in root:

这是根目录下的 urls.py:

mysite/urls.py

我的网站/urls.py

from django.conf.urls import patterns,include, url
from django.contrib import admin

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

I've spent good piece of time to find out the solution, found that this question has been asked for many times,so tried with those solutions as well but none of them worked for me.

我花了很多时间来找出解决方案,发现这个问题已经被问过很多次了,所以也尝试了这些解决方案,但没有一个对我有用。

I can't find out what I'm missing here so please draw my attention to the gap.

我找不到我在这里遗漏了什么,所以请让我注意这个差距。

采纳答案by Alasdair

I think you have edited the wrong file when trying to change the root url config.

我认为您在尝试更改根 url 配置时编辑了错误的文件。

Make sure you are editing the root url config in mysite/mysite/urls.py(the directory containing settings.py) not mysite/urls.py(the directory containing manage.py).

确保您在mysite/mysite/urls.py(包含 的目录settings.py)而不是mysite/urls.py(包含 的目录)中编辑根 url 配置manage.py

As general advice, install the latest release, currently 1.9. Don't use 1.10, which is under development. Make sure that you are following the tutorial for 1.9, because the tutorial changes for different versions. For example, your mysite/urls.pydoesn't match the tutorial for 1.9, as the urlpatternsshould be:

作为一般建议,请安装最新版本,目前为 1.9。不要使用正在开发的 1.10。确保您遵循1.9的教程,因为教程会因不同版本而异。例如,您mysite/urls.py与 1.9 的教程不匹配,因为urlpatterns应该是:

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

回答by utkbansal

In settings.pyyou have a setting name INSTALLED_APPS-

settings.py你有一个设置名称 INSTALLED_APPS-

Adds you app i.e. pollsto it.

将您的应用程序添加polls到其中。

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    ....
    'polls',
]

回答by adebayo

I too had same problem going through the official docs tutorial. I was using cloud 9. What I realised before I was able to solve this problem was that while creating the workspace I already chose django(ie by the time my workspace was created, django had already been installed) And going through the tutorial, I again executed $ django-admin startproject mysitethereby having another layer of django with multiple directories and unavoidably the confusion. My solution was to delete everything and start all over.

我在阅读官方文档教程时也遇到了同样的问题。我使用的是 cloud 9。在我能够解决这个问题之前,我意识到在创建工作区时我已经选择了 django(即在我的工作区创建时,django 已经安装)并且通过教程,我再次执行,$ django-admin startproject mysite从而拥有另一层带有多个目录的 django,并且不可避免地出现了混乱。我的解决方案是删除所有内容并重新开始。

Since this problem is common to many people, I suggest that django tutorial docs should be more explicit on this.

由于这个问题对很多人来说都很常见,我建议 django 教程文档应该更明确地说明这一点。

I hope this help somebody.

我希望这对某人有帮助。

回答by adebayo

if @Alasdair answer does not work, and it seems your working on correct files, just restart your server

如果@Alasdair 回答不起作用,并且您似乎正在处理正确的文件,只需重新启动服务器

回答by Rocky D H??k?rboy

It's working. Go to your url bar and type your app name:

它正在工作。转到您的网址栏并输入您的应用名称:

http://127.0.0.1:8000/home

My app name is home. It will work.

我的应用程序名称是 home。它会起作用。

If you want to set your app as your default page then import views from your app.

如果您想将您的应用程序设置为默认页面,请从您的应用程序导入视图。

Like

喜欢

from home import views

then write

然后写

url(r'^$', views.index),

inside.

里面。

It will set the views as your default page

它将视图设置为您的默认页面

http://127.0.0.1:8000/

When you type this it will redirect to your views.

当您键入此内容时,它将重定向到您的视图。

回答by ru13r

I had the same problem as described. Running a Windows machine.

我遇到了与描述相同的问题。运行 Windows 机器。

It turned out, that the virtual environment I was using had not been configured properly (or maybe I had forgot to activate it before installing Django) and the interpreter and django-admin were fetched from the wrong path by CMD.EXE.

结果,我使用的虚拟环境没有正确配置(或者我可能在安装 Django 之前忘记激活它)并且解释器和 django-admin 被 CMD.EXE 从错误的路径中获取。

If it appears as if Django is "ignoring" your urls.py- try deleting everything, re-creating the virtual environment, activating it and re-installing Django afterwards.

如果看起来 Django 正在“忽略”您的urls.py- 尝试删除所有内容,重新创建虚拟环境,激活它并在之后重新安装 Django。

Hope this helps.

希望这可以帮助。

回答by Bello Tomi

change the mysite/url

更改我的网站/网址

    from django.conf.urls import patterns,include, url
    from django.contrib import admin

    urlpatterns = patterns('',
    url(r'^&', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
    )

Then run your server and visit 127.0.0.1/8000. This should take you to the index of ur website.

然后运行你的服务器并访问 127.0.0.1/8000。这应该带您到您网站的索引。

or you leave your code as it is and run 127.0.0.1/8000/polls on your browser

或者您保持代码不变并在浏览器上运行 127.0.0.1/8000/polls

回答by sheetal singh

checklist:

清单:

  1. make sure to run the server
  1. 确保运行服务器
>>python manage.py runserver

Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
February 01, 2020 - 15:04:46
Django version 3.0.2, using settings 'corona.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

使用 StatReloader 监视文件更改 正在
执行系统检查...
系统检查未发现任何问题(0 静音)。
2020 年 2 月 1 日 - 15:04:46
Django 版本 3.0.2,使用设置“corona.settings”
启动开发服务器http://127.0.0.1:8000/
使用 CTRL-BREAK 退出服务器。

2.open the browser with cmd running, http://127.0.0.1:8000/pollsshould be added to the url.

2.打开浏览器,运行cmd, url中添加http://127.0.0.1:8000/ polls