2016-06-20 70 views
0

當我打開我的網站,我看到這一點, 我創建的項目(mysite的)和應用(調查)的Python(Django的)錯誤 - 找不到網頁(404)

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. 

我的文件 投票/意見的.py

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

民調/ 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. 

回答

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/

+0

謝謝,慢慢我開始理解Django – woljako