【问题标题】:Django ImproperlyConfigured at /Django 配置不当在 /
【发布时间】:2014-08-24 04:00:40
【问题描述】:

我刚刚建立了一个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,但没有帮助。有什么问题以及如何解决?

【问题讨论】:

    标签: django django-urls


    【解决方案1】:

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

    要纠正问题,您可以这样做:

    # 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)),
    ]
    

    【讨论】:

      【解决方案2】:

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

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

      include() 用于从另一个模块/应用程序就地“包含”url 配置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-07
        • 1970-01-01
        • 1970-01-01
        • 2013-03-11
        • 2013-10-08
        • 2011-05-09
        相关资源
        最近更新 更多