Reputation: 633
excuese me everyone: I am trying to practice Django by a book "light weight Django",however I got the error 'module' object has no attribute 'SITE_PAGES_DIRECTORY' .
error:
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'),
)
can anyone tell me why it has error please,very thanks!!
Upvotes: 0
Views: 255
Reputation: 1471
The line
SITE_PAGES_DIRECTORY=os.path.join(BASE_DIR,'pages')
should be inside
settings.configure()
not outside, i.e.
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
)
Upvotes: 1