Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No FlatPages matches the given query

Tags:

python

django

I mad the necessary changes in settings.py for flatpages. Then in admin I create a flatpage /about/. But when I am running the http://localhost:8000/about/ it renders 404 that is No FlatPages matches the given query. My default.html is in project/templates/flatpages/default.html.

urls.py

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
    (r'^(?P<url>about/)$', 'django.contrib.flatpages.views.flatpage'),
)

settings.py

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

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'django.contrib.flatpages',)
like image 670
aoondey Avatar asked Feb 26 '23 17:02

aoondey


2 Answers

It looks like you might want to read up on named groups a bit.

But for now try putting this in your urls.py instead:

# last entry in urls.py.  letting flatpages handle found pages or throw 404's
(r'', include('django.contrib.flatpages.urls')),

That just makes sure we have the urls linked up properly. Next lets take a look at how we create the FlapPage objects. Via the /admin is the easiest but you can also use the shell. The trick when creating these by hand is to add a site.

./manage.py shell
> from django.contrib.flatpages.models import FlatPage
> fp = FlatPage.objects.create(url="/test/", content="This is a test...")
> fp.sites.add(1) # in dev you are probably using site.id == 1.

Optionally you can try to use your existing FlatPage object. Just make sure it is associated with a site.

> about = FlatPage.objects.get(url="/about/")
> about.sites.all()
[]
> # Empty list of sites.  This will give you a 'No FlatPage matches' 404.
> about.sites.add(1)

Now fire up the server and see how it goes. If you have not created a flatpages/default.html template you will get an TemplateDoesNotExist exception.

like image 182
istruble Avatar answered Mar 06 '23 23:03

istruble


After following most of the solutions provided, the most important factor you should consider that is not indicated in the django docs is that of SITE_ID = 1.

By default Django provides on the admin panel for Sites, "example.com" . When adding flatpages one may tend to add his own site and most tutorials will dictate that you use SITE_ID = 1 on settings.py .

What the don't tell you is that "example.com" site is by default 1. SO what you should do is edit the "example.com" site to 127.0.0.1:8000 and there you have it.

Remember to also add url.py

# last entry in urls.py.  

(r'', include('django.contrib.flatpages.urls')),

settings.py`

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'

)

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.flatpages',

)

like image 24
kenju254 Avatar answered Mar 06 '23 21:03

kenju254