Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django flatpages Do Not Work

Tags:

python

django

I'm reading and following the book "Practical Django Projects: Second Edition" and a lot of the book already is out of date to the point where an entire chapter won't work at all. Right now I'm trying to create my first flat page and I'm getting a 404 error.

I'm trying to use the built in 'django.contrib.flatpages' app and I've already created my first flatpage via the admin panel:

enter image description here

Here's my urls.py file:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',


    url(r'^admin/', include(admin.site.urls)),
    (r'', include('django.contrib.flatpages.urls')) #ADDED THIS LINE FOR FLATPAGES
)

Here's what I've done to my settings.py file so far:

SITE_ID = 1 # REQUIRED FOR 'django.contrib.flatpages'


INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.flatpages', # ADDED FOR FLATPAGES
    'south',
)

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',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware', #FOR FLATPAGES
)

So that's about all that I've done so far and I'm expecting a "TemplateDoesNotExist at /first-page/" when navigating to like the book says: http://example.com:1006/first-page/

But instead of that, I'm simply getting a 404 error.

Am I doing this correctly or did a new Django version depreciate the way to make flatpages?

like image 710
user1504605 Avatar asked Feb 13 '23 19:02

user1504605


1 Answers

I finally figured it out, and every resource online doesn't mention this at all. They never even cover this stuff in the entire chapter from the Apress book. Hopefully everyone else from here on out doesn't have to spend 3 hours on this solution.

enter image description here

So the solution is, make sure that the SITE_ID in the settings.py file matches the number in the URL of the site when clicking "change site" in the admin panel. As you can see in the image above, for me the number is "4" which is the site ID. So my settings.py should look like this:

SITE_ID = 4

There are many threads like this one where people just say "change it to 1", which is misleading. Make sure the number matches your site ID!

like image 150
user1504605 Avatar answered Feb 16 '23 11:02

user1504605