No FlatPages matches the given query
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.contri开发者_StackOverflow社区b.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',)
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.
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',
)
If you have changed (added new ) site in the sites application , you have to change SITE_ID
in settings.py with the value which matches the newly added site .
For some reason only if you use site_id = 1 will it work.
This drove me up the wall for a while, but it might be what you got stuck on. When you define a page on the admin site, if you don't include a / after the name of the site it'll give you a 404.
ie. URL: /first-page/ vs /first-page
the first version will work, while the 2nd one will 404. It might be what you're stuck on? If not, just posting this for other people that might be stuck on the same issue that was bugging me.
This assumes you've already created one row in FlatPage. Follow Django Unleashed 2016 chapter 15. Not sure I got it. Apparently we already have a row in Site (and apparently this happens behind the curtain during migration) , but we have to create one in FlatPage and then add our sole Site object to that FlatPage's site field: (in shell)
from django.contrib.flatpages.models import FlatPage, Site
fp=FlatPage.objects.get(pk=1)
our_site=Site.objects.all()[0]
fp.sites.add(our_site)
fp.save()
精彩评论