Python(Django)错误 - 找不到页(404)

时间:2016-06-20 19:42:04

标签: python django

当我打开我的网站时,我看到了这个, 我创建了项目(mysite)和应用程序(民意调查)

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

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

^polls/
^admin/

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

我的档案 轮询/ views.py

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

polls / urls.py

from django.conf.urls import url
from . import views

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

的mysite / urls.py

import os
import sys

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

import django

sys.path.append("/home/ukasz/Pulpit/Python/mysite") #Set it to the root of your project
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
django.setup()

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

的mysite / settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
]

我不得不添加django.setup()和os.environ,因为我遇到了错误:

django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

1 个答案:

答案 0 :(得分:0)

您需要在mysite / urls.py文件中定义路由。目前,

def index(request) 
点击http://127.0.0.1:8000/polls

有效。你应该添加

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

到你的mysite / urls.py来处理默认的http://127.0.0.1:8000/