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条回答
女痞
2楼-- · 2019-01-22 18:41

Add the below line in your Mysite/urls.py

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

and check. If you have created your project correctly, it should work. Else something like above might have happened to have more than one files so confused.

查看更多
家丑人穷心不美
3楼-- · 2019-01-22 18:43

Depending on where you put your ROOT urls.py, you set your ROOT_URLCONFIG accordingly, if you have it in your outermost folder containing manage.py then "urls" is ok. if you have it in someother folder then you have to do ".urls"

Credit for the answer to jerryh91

For more info about how it works, check How Django processes a request

查看更多
叼着烟拽天下
4楼-- · 2019-01-22 18:45

You put the urls.py folder into the outer MySite folder, you are suppose to put it in the inner one so its not mySite/urls.py, but mySite/mySite/urls.py:

ran into the same mistake when i did the tutorial

查看更多
够拽才男人
5楼-- · 2019-01-22 18:45

In my case, it was a stupid mistake. I wanted to integrate the plugin django-tinymce, and test it. So following this guide, I did the step 3 and exported the variable to the path. As the server runned again, I received the not found error, showing the message:

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

But I didn't know what exactly it was, until I remembered exporting the variable DJANGO_SETTINGS_MODULE

running unset DJANGO_SETTINGS_MODULE in terminal solved my issue. Hope that it helps someone too.

查看更多
Lonely孤独者°
6楼-- · 2019-01-22 18:47
from django.conf.urls import include, url
from django.contrib import admin
from polls import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^polls/', include('polls.urls', namespace='polls')),
    url(r'^$', views.index, name='index'),
]
查看更多
登录 后发表回答