Page not found 404 on Django site?

2019-01-22 18:00发布

I'm following the tutorial on Django's site to create a simple poll app. However, Django is unable to resolve "//127.0.0.1:8000/polls" , even though I've defined the regex in mySite/urls.py. I'm doing this in a virtualenv, with the latest Django (1.7) installed.

mySite/urls.py:

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

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

mySite/polls/urls.py:

from django.conf.urls import patterns, url
from polls import views

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

mySite/polls/views.py:

from django.shortcuts import render
from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

mySite/settings.py:

 ...
 INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
 ) 
  ....
 ROOT_URLCONF = 'mySite.urls'

The error I'm getting:

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.

11条回答
小情绪 Triste *
2楼-- · 2019-01-22 18:23

2017-10-05_12:03 ~/mysite/mysite
$ vi urls.py 2017-10-05_12:04 ~/mysite/mysite
$ cd ../.. 2017-10-05_12:04 ~
$ mv mysite SENSIBLE_NAME_DJANGO_ROOT

查看更多
迷人小祖宗
3楼-- · 2019-01-22 18:25

You're accessing to http://yourdomain.com/, and you don't have any URL defined for "/".

You have two options:

  1. If you want to access to the index page of your polls application you have to enter the URL: yourdomain.com/polls

  2. You can also modify you mySite/urls.py file to access from just yourdomain.com

    from django.conf.urls import patterns, include, url
    
    from django.contrib import admin
    urlpatterns = patterns('',
    
    url(r'^admin/', include(admin.site.urls)),
    
    url(r'^$', include('polls.urls')),
    
    )
    
查看更多
混吃等死
4楼-- · 2019-01-22 18:26

To make the answer clear for beginners who has this issue by following the tutorial, the project root URLconf is the one in the same folder as settings.py which is:

 mysite/mysite/urls.py

Just make sure import 'include'. The code looks like:

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

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

So in mysite/mysite/settings.py:

The line should be:

ROOT_URLCONF = 'mysite.urls'

You don't need create a fresh new root URLconf.

查看更多
太酷不给撩
5楼-- · 2019-01-22 18:30

Actually the problem is that you didn't notice that mysite/urls.py and polls/urls.py are two different files and you modified polls/urls.py instead of putting mysite/urls.py in the urls.py file in ...mysite\mysite folder.

查看更多
Anthone
6楼-- · 2019-01-22 18:32

Django unable to resolve 127.0.0.1:8000/polls because url config defined as r'^polls/'.

Usual workaround:

mySite/urls.py:

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

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

Note: Whenever Django encounters include(), It chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

mySite/polls/urls.py:

from django.conf.urls import patterns, url
from polls import views

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

Note: Instead of typing that out for each entry in urlpatterns, you can use the first argument to the patterns() function to specify a prefix to apply to each view function.

Answer If

If you want to access 127.0.0.1:8000/polls Note: without trailing slash

use view based url

url(r'^polls', 'polls.views.index', name='index'),

So now you can access 127.0.0.1:8000/polls without trailing slash.

查看更多
劫难
7楼-- · 2019-01-22 18:37

I had the same problem.

It turns out I was confused because of the multiple directories named "mysite".

I wrongly created a urls.py file in the root "mysite" directory (which contains "manage.py"), then pasted in the code from the website.

To correct it I deleted this file, went into the mysite/mysite directory (which contains "settings.py"), modified the existing "urls.py" file, and replaced the code with the tutorial code.

In a nutshell, make sure your urls.py file is in the right directory.

查看更多
登录 后发表回答