juanli
juanli

Reputation: 613

Page not found: http://127.0.0.1:8000/sitemap.xml/

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/ enter image description here

enter image description here

There is the url (the eighth) matching my inputted one. Why it says 'not match'?

Upvotes: 0

Views: 1346

Answers (3)

Lautaro Parada Opazo
Lautaro Parada Opazo

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:

  1. Check if you are running the local server in your command prompt, if is not, type python manage.py runserver.
  2. go to http://127.0.0.1:8000/admin/sites/site/
  3. Change the default site (typically the default in Django is 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).
  4. Press F5 in your local web page in your browser.
  5. go to your sitemap page (http://127.0.0.1:8000/sitemap.xml)
  6. And voilà! there is your sitemap.

Upvotes: 5

Borut
Borut

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:

  1. Add 'django.contrib.sites' to your INSTALLED_APPS setting.
  2. Define a SITE_ID setting:

    SITE_ID = 1

  3. Run migrate.

For more information check documentation.

Upvotes: 1

Humza Jalal
Humza Jalal

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

Related Questions