Django在/处不正确地配置

时间:2014-08-24 04:00:41

标签: django django-urls

我刚刚设置了django项目并收到错误:

Request Method: GET
Request URL:    http://127.0.0.1/hello/
Django Version: 1.6.6
Exception Type: ImproperlyConfigured
Exception Value:    
The included urlconf <function hello at 0x7f665a1e0320> doesn't have any patterns in it
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in url_patterns, line 369
Python Executable:  /usr/local/bin/uwsgi
Python Version: 2.7.6

urls.py:

from django.conf.urls import patterns, include, url
import testsite.views
from django.contrib import admin
admin.autodiscover()

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

views.py:

from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello, world!")

settings.py:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# SECURITY WARNING: keep the secret key used in production sexbbiv*#q44x+jdawuchyu_!$wd#f-p(hid2r*zrjvy6a6#bsoxbbiv*#q44x+jdawuchyu_!$wd#f-p(hid2r*zrjvy6a6#bsocret!
SECRET_KEY = '##############&'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'testsite.urls'

WSGI_APPLICATION = 'testsite.wsgi.application'

# Database

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'

我已经开始搜索设置DEBUG_TOOLBAR_​​PATCH_SETTINGS = False,但它没有帮助。问题是什么以及如何解决?

2 个答案:

答案 0 :(得分:1)

不要将include()用于视图模式:

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

include()用于“包含”来自其他模块/应用程序的url配置。

答案 1 :(得分:1)

错误很明显:它不是有效的网址配置。

要解决此问题,您可以这样做:

# app/views.py
from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello, world!")

现在在该应用中创建一个名为urls.py的文件:

# app/urls.py
from django.conf.urls import url

from app import views

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

现在终于在site/urls.py

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

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