'模块'对象没有属性' SITE_PAGES_DIRECTORY'

时间:2015-12-03 09:30:55

标签: python django

让每个人都说: 我正在尝试用一本书来练习Django"轻量级Django"但是我得到了错误'模块'对象没有属性' SITE_PAGES_DIRECTORY'

错误:

enter image description here 工作树: enter image description here

prototypes.py:

import os
import sys
from django.conf import settings

BASE_DIR = os.path.dirname(__file__)
STATIC_URL='/static/',
SITE_PAGES_DIRECTORY=os.path.join(BASE_DIR,'pages'),

settings.configure(
    DEBUG = True,
    SECRET_KEY= 'qfzf!b1z^5dyk%syiokeg4*2xf%kj68g=o&e8qvd@@(1lj5z8)',
    ROOT_URLCONF='sitebuilder.urls',
    MIDDLEWARE_CLASSES=(),
    INSTALLED_APPS=(
        'django.contrib.staticfiles',
        'sitebuilder',
    ),
    TEMPLATES=(
        {
            'BACKEND':'django.template.backends.django.DjangoTemplates',
            'DIRS':[],
            'APP_DIRS':True,
        },
    ),
    STATIC_URL='/static/',
)

if __name__ == "__main__":
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

views.py:

import os

from django.conf import settings
from django.http import Http404
from django.shortcuts import render
from django.template import Template
from django.utils._os import safe_join


def get_page_or_404(name):
    try:
        file_path = safe_join(settings.SITE_PAGES_DIRECTORY, name)
    except ValueError:
        raise Http404('Page Not Found')
    else:
        if not os.path.exists(file_path):
            raise Http404('Page Not Found')

    with open(file_path, 'r') as f:
        page = Template(f.read())
    return page


def page(request, slug='index'):
    file_name = '{}.html'.format(slug)
    page = get_page_or_404(file_name)
    context = {
        'slug':slug,
        'page':page,
    }
    return render(request, 'page.html', context)

urls.py:

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

urlpatterns = (
    url(r'^(?P<slug>[\w./-]+)/$', page, name='page'),
    url(r'^$', page, name='homepage'),
)

任何人都可以告诉我为什么它有错误,非常感谢!!

1 个答案:

答案 0 :(得分:1)

该行

SITE_PAGES_DIRECTORY=os.path.join(BASE_DIR,'pages')

应该在里面

settings.configure()

不在外面,即

settings.configure(
    DEBUG = True,
    SECRET_KEY= 'qfzf!b1z^5dyk%syiokeg4*2xf%kj68g=o&e8qvd@@(1lj5z8)',
    ROOT_URLCONF='sitebuilder.urls',
    MIDDLEWARE_CLASSES=(),
    INSTALLED_APPS=(
        'django.contrib.staticfiles',
        'sitebuilder',
    ),
    TEMPLATES=(
        {
            'BACKEND':'django.template.backends.django.DjangoTemplates',
            'DIRS':[],
            'APP_DIRS':True,
        },
    ),
    STATIC_URL='/static/',
    SITE_PAGES_DIRECTORY=os.path.join(BASE_DIR,'pages'), #RIGHT HERE
)