Reputation: 613
Here are the codes to create the sitemap for one of my app 'blog' in my site: (using Django 2.0)
settings.py
INSTALLED_APPS += [
'django.contrib.sites',
'django.contrib.sitemaps',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
urls.py
from django.urls import include, path
from django.contrib.sitemaps.views import sitemap
from blog.sitemaps import PostSiteMap
sitemaps = {'posts': PostSiteMap}
urlpatterns += [
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap')
]
sitempas.py (under 'blog' app directory)
from django.contrib.sitemaps import Sitemap
from .models import Post
class PostSiteMap(Sitemap):
changefreq = 'weekly'
priority = 0.5
def items(self):
return Post.published.all()
def lastmod(self, obj):
return obj.publish
The sitemap.xml doesn't appear: http://127.0.0.1:8000/sitemap.xml/
There is the url (the eighth) matching my inputted one. Why it says 'not match'?
Upvotes: 0
Views: 1346
Reputation: 259
Your code is good, your environment is what is wrong (maybe you are using the default Django site, that is www.example.com). Change it to a local environment (the typical 127.0.0.1:8000
).
To do this in Django you need to do the following:
python manage.py runserver
.http://127.0.0.1:8000/admin/sites/site/
example.com
) to 127.0.0.1:8000
. This needs to be done in the domain and display name (while you aren't in a production environment).F5
in your local web page in your browser. http://127.0.0.1:8000/sitemap.xml
)Upvotes: 5
Reputation: 3364
The error is
Site matching query does not exist
which means that you have to setup and configure Sites framework.
To enable the sites framework, follow these steps:
Define a SITE_ID setting:
SITE_ID = 1
Run migrate.
For more information check documentation.
Upvotes: 1
Reputation: 27
Write full /sitemap.xml like this and try Enter this url and load page http://127.0.0.1:8000/sitemap.xml/
Upvotes: 1