Why do I get this `This XML file does not appear to have any style information associated with it. The document tree is shown below.`

Following through on a turorial on building a blog with Django, I did as instructed and the result matched theirs save for the first line, this line
This XML file does not appear to have any style information associated with it. The document tree is shown below.

What am I doing wrong?

Generally speaking this means you either have something wrong in your template, or you’re not returning the right headers in your http response, or you’re not returning an HTML page.

We’d need to see a lot more of your code to figure out what’s going wrong, and a reference to the tutorial you’re following would be helpful, too.

We would need to see your view and templates, and possibly your urls.py file, too.

Note: when you post code here, please enclose it between lines consisting of only three backtick - ` characters. That means you’ll have one line of ```, then your code, then another line of ```. This preserves the formatting of the code making it a lot easier to read - especially with statements where indentation is critical.


# The line above this is ```
def function(self):
    return self
# The line after this is ```


1 Like

The tutorial is from the textbook ‘Django 3 by example’, Chapter 3 Extending your blog application

Here is the sitemaps.py of the blog app. The project has 2 apps, one, the polls app from Writing your first app on here and the blog app from the textbook.

from django.contrib.sitemaps import Sitemap
from .models import Post

class PostSitemap(Sitemap):
    changefreq = 'weekly'
    priority = 0.9
    def items(self):
        return Post.published.all()
    def lastmod(self, obj):
        return obj.updated

I added a SITE_ID as instructed (though, I don’t know what the SITE_ID really does), added django.contrib.sites and django.contrib.sitemaps to the INSTALLED_APPS

Here is my project’s urls.py, not the blog app’s urls.py

from django.contrib import admin
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},\
    path('admin/', admin.site.urls),
    path('polls/', include('polls.urls', namespace="polls")),
    path('blog/', include('blog.urls', namespace='blog'))

Here is the base.html (my base template)

{% load blog_tags %}
{% load static %}
<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html>
    <html lang="en-us">
        <meta charset="utf-8">
        <title>{% block title %}{% endblock %}</title>
        <link href="{% static "css/blog.css" %}" type="text/css" rel="stylesheet">
        <div id="content">
            {% block content %}
            {% endblock %}
        <div id="sidebar">
            <h2><a href="{% url 'blog:post_list' %}">My Blog</a></h2>
            <p>This is my blog, I have written {% total_post %} posts so far.</p>
            <p><a href="{% url "blog:post_feed" %}">Subcribe to my feed</a></p>
            <h3>Latest Posts</h3>
            {% show_latest_posts 3 %}
            <h3>Most Commented Posts</h3>
            {% get_most_commented_posts as most_commented_posts %}
              {% for post in most_commented_posts %}
                <li><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></li>
              {% endfor %}

Here is the post_list view from my views.py that renders the page and (which from the view passed to path in the project’s urls.py is not the one rendering the sitemap)

def post_list(request, tag_slug=None):
    object_list = Post.published.all()
    tag = None

    if tag_slug:
        tag = get_object_or_404(Tag, slug=tag_slug)
        object_list = object_list.filter(tags__in=[tag])

    paginator = Paginator(object_list, 3) # 3 posts in each page
    page = request.GET.get('page')
        posts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer deliver the first page
        posts = paginator.page(1)
    except EmptyPage:
        # If page is out of range deliver last page of results
        posts = paginator.page(paginator.num_pages)
    return render(request,
                 {'page': page,
                  'posts': posts,
                  'tag': tag})

What URL is generating the message that you’re asking about? Is it the sitemap page or is it your post_list view? (And why do you have xml and doctype entries in your template? Is that shown in the tutorial you’re using?)

1 Like

It is the root urls.py file, it has the path to the xml page and the appropriate view (sitemap which was imported).
The specified atrributes and methods in the sitemap.py file of the app’s directory don’t throw an error. And from the resulting page after opening my localhost/sitemap.xml has the same tree as the tutorials except for that message I posted above which appears at the top of my page, then a underline, then the tree.

The xml tag was added after I queried Google and I came across a solution to a similar problem which suggested the tag and some others be added. I removed others when the message wouldn’t stop showing.

The tutorial included DOCTYPE in its instruction.

After adding the apps to INSTALLED_APPS, did you run python manage.py migrate?

1 Like

First, for clarity, your urls.py file does not generate a page. It’s only the mechanism that your server process uses to find the view which does generate a page. In this case, the view is the sitemap utility.

But, most importantly, what you’re seeing as a response to the sitemap.xml url is not an error.
The intent of a sitemap.xml file is not to produce a human-readable page - it’s designed for use as an api endpoint for automated retrieval and processing. The fact that you’re getting that message just means that the browser doesn’t have any predefined stylesheet for that file.

1 Like

I did run it and it returned ‘ok’ responses.

Okay, but please what does the SITE_ID really do?

I tried commenting it out and ran the sitemap.xml page and I got an error. I changed the value from 1 to 2, still got an error.

Is the SITE_ID an identifier for a particular app or project because I have two apps installed in my INSTALLED_APPS in the project’s settings.py file.

It’s not associated with an app, it’s a reference to a site. See the Sites Framework docs.

1 Like