为什么不是'显示网址的Django站点地图



我一直在努力让我的django站点地图适用于一个小型博客应用程序。

在查看了多个教程后,我仍然无法使网站地图正常工作。

这是我的网址.py:

from django.contrib import admin, sitemaps
from django.urls import path
from django.urls import include
from rx import views
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.sitemaps.views import sitemap
from rx.sitemaps import Static_Sitemap
sitemaps = {
'static': Static_Sitemap,
}

urlpatterns = [
path('ckeditor/', include('ckeditor_uploader.urls')),
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('blog/', views.blog, name='blog'),
path('about/', views.about, name='about'),
path('<slug:cat_slug_name>/', views.cat, name='cat'),
path('<slug:cat_slug_name>/<slug:post_slug_name>/', views.blog_post, name='blog_post'),
path('sitemap.xml/', sitemap, {'sitemaps': sitemaps}),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我的设置.py:

from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR, 'static')
MEDIA_DIR = os.path.join(BASE_DIR, 'media')

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rx',
'ckeditor',
'ckeditor_uploader',
'django.contrib.sitemaps',
'django.contrib.sites',
]
SITE_ID = 1
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'rankxero.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR, ],
'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',
],
},
},
]
WSGI_APPLICATION = 'rankxero.wsgi.application'

# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]
MEDIA_URL = '/media/'
MEDIA_ROOT = MEDIA_DIR
CKEDITOR_UPLOAD_PATH = 'uploads/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

这是Models.py:

from django.db import models
from django.template.defaultfilters import slugify
from django.utils.timezone import now
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
from django.urls import reverse

class Category(models.Model):
title = models.CharField(max_length=128, unique=True)
subtitle = models.CharField(max_length=512, unique=True)
slug = models.SlugField(unique=True)
posts_in_category = models.CharField(max_length=128, default=0)
meta_description = models.CharField(max_length=160, blank=True)
sitemap_url = models.CharField(max_length=128, blank=True)
class Meta:
verbose_name_plural = '1. Categories'
def __str__(self):
return self.title
class Post(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
post_name = models.CharField(max_length=128, unique=True)
slug = models.SlugField(unique=True, blank=True, null=True)
featured_image = models.ImageField(blank=True)
content = RichTextUploadingField(default="Write the beginning of your article here")
author = models.CharField(max_length=15, default="Salman")
date = models.DateTimeField(default=now)
date_modified = models.DateTimeField(default=now)
reading_time = models.CharField(max_length=128, blank=True)
meta_description = models.CharField(max_length=200, blank=True, null=True)
sitemap_url = models.CharField(max_length=128, blank=True)
class Meta:
verbose_name_plural = '2. Posts'

def __str__(self):
return self.post_name
def get_absolute_url(self):
return f'{self.category}/{self.slug}/'  

class About(models.Model):
featured_image = models.ImageField(blank=True)
name = models.CharField(max_length=128)
content = RichTextField(default="Write the beginning of your article here")
slug = models.SlugField(unique=True, blank=True, null=True)
class Meta:
verbose_name_plural = '3. About'

def __str__(self):
return self.name
def get_absolute_url(self):
return f'/{self.slug}'    

这是sitemaps.py文件:

from django.contrib import sitemaps
from django.urls import reverse
from django.contrib.sitemaps import Sitemap

class Static_Sitemap(Sitemap):
priority = 1.0
changefreq = 'yearly'

def items(self):
return ['index', 'about', 'blog']

def location(self, item):
return reverse(item)

我只是想把静态页面添加到网站地图中。还没有开始使用动态页面。

网站地图确实显示了结果,但这就是我得到的:

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.example.com/</loc>
<lastmod>2004-10-01T18:23:17+00:00</lastmod>
</sitemap>
<sitemap>
<loc>http://www.example.com/sitemap2.xml.gz</loc>
<lastmod>2005-01-01</lastmod>
</sitemap>
</sitemapindex>

有时URL没有索引,或者可能存在冲突。添加名称将清除此你能试着在url 中添加名称吗

path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap') 

我遇到了同样的问题,但解决方案有点不同,所以我想我会发布。

我不得不将路径更改为"站点地图">

path('sitemap', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap') 

最新更新