当前的URL, app/,与其中任何一个都不匹配。

时间:2022-02-17 02:54:34

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:

我已经遵循了这个链接的所有指示。但是我不断得到这个错误

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:

为了开始,我同意了。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

民意调查/ urls . 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:

这是url。py根:

mysite/urls.py

mysite / 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.

我不知道我在这里错过了什么,请把我的注意力转移到这个差距上。

8 个解决方案

#1


14  

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/url中编辑根url配置。py(包含settings.py的目录)不是mysite/url。py(包含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.py doesn't match the tutorial for 1.9, as the urlpatterns should be:

作为一般建议,安装最新版本,当前1.9。不要使用正在开发中的1.10。请确保您遵循了1.9的教程,因为本教程将针对不同的版本进行更改。例如,您的mysite / url。py与1.9的教程不匹配,因为urlpatterns应该是:

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

#2


4  

In settings.py you have a setting name INSTALLED_APPS-

在设置。py有一个设置名称INSTALLED_APPS-。

Adds you app i.e. polls to it.

添加你的应用,即投票。

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

#3


1  

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中测试。

http://localhost:8000/polls/

http://localhost:8000 /调查/

Please download a working "HelloWorld" example in django 1.10

请下载django 1.10中的一个工作“HelloWorld”示例。

https://mega.nz/#!qFsgBbRD!lAKKprkMLmhJcaZ8h5NPxkZcfQpB8qrh1nU4rSSaRAo

https://mega.nz/ qFsgBbRD ! lAKKprkMLmhJcaZ8h5NPxkZcfQpB8qrh1nU4rSSaRAo

#4


1  

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.

我的app名是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.

当您键入这个时,它将重定向到您的视图。

#5


0  

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 mysite thereby 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.

我希望这对某人有帮助。

#6


0  

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

如果@Alasdair的答案不起作用,而且看起来您的工作在正确的文件上,重新启动您的服务器。

#7


0  

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是从错误的路径中提取的。

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是在“忽略”您的url。py -尝试删除一切,重新创建虚拟环境,激活它,然后重新安装Django。

Hope this helps.

希望这个有帮助。

#8


0  

change the mysite/url

改变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/投票。

#1


14  

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/url中编辑根url配置。py(包含settings.py的目录)不是mysite/url。py(包含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.py doesn't match the tutorial for 1.9, as the urlpatterns should be:

作为一般建议,安装最新版本,当前1.9。不要使用正在开发中的1.10。请确保您遵循了1.9的教程,因为本教程将针对不同的版本进行更改。例如,您的mysite / url。py与1.9的教程不匹配,因为urlpatterns应该是:

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

#2


4  

In settings.py you have a setting name INSTALLED_APPS-

在设置。py有一个设置名称INSTALLED_APPS-。

Adds you app i.e. polls to it.

添加你的应用,即投票。

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

#3


1  

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中测试。

http://localhost:8000/polls/

http://localhost:8000 /调查/

Please download a working "HelloWorld" example in django 1.10

请下载django 1.10中的一个工作“HelloWorld”示例。

https://mega.nz/#!qFsgBbRD!lAKKprkMLmhJcaZ8h5NPxkZcfQpB8qrh1nU4rSSaRAo

https://mega.nz/ qFsgBbRD ! lAKKprkMLmhJcaZ8h5NPxkZcfQpB8qrh1nU4rSSaRAo

#4


1  

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.

我的app名是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.

当您键入这个时,它将重定向到您的视图。

#5


0  

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 mysite thereby 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.

我希望这对某人有帮助。

#6


0  

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

如果@Alasdair的答案不起作用,而且看起来您的工作在正确的文件上,重新启动您的服务器。

#7


0  

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是从错误的路径中提取的。

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是在“忽略”您的url。py -尝试删除一切,重新创建虚拟环境,激活它,然后重新安装Django。

Hope this helps.

希望这个有帮助。

#8


0  

change the mysite/url

改变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/投票。