我如何在 django 中编写动态站点地图,我的不工作
How can I program a dynamic sitemap in django, mine not working
静态部分工作正常,但我的博客应用程序的动态帖子未生成
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from .models import Post
class PostSitemap(Sitemap):
changefreq = "weekly"
priority = 0.9
def items(self):
return Post.objects.all()
我无法通过我的博客和模型中的 Post class 获取动态站点地图:
class Post(models.Model):
title=models.CharField(max_length=100)
header_image = models.ImageField(null=True , blank=True, upload_to="images/")
title_tag=models.CharField(max_length=100)
author= models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextUploadingField(extra_plugins=
['youtube', 'codesnippet'], external_plugin_resources= [('youtube','/static/ckeditor/youtube/','plugin.js'), ('codesnippet','/static/ckeditor/codesnippet/','plugin.js')])
#body = models.TextField()
post_date = models.DateTimeField(auto_now_add=True)
category = models.CharField(max_length=50, default='uncategorized')
snippet = models.CharField(max_length=200)
likes = models.ManyToManyField(User, blank=True, related_name='blog_posts')
def total_likes(self):
return self.likes.count()
class Meta:
verbose_name = "Entrada"
verbose_name_plural = "Entradas"
ordering = ['-post_date']
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('home')
如果有人能帮助我,我将不胜感激。
urls.py:
from django.contrib.sitemaps.views import sitemap
from theblog.sitemaps import PostSitemap, StaticSitemap
sitemaps = {'static': StaticSitemap, 'blog': PostSitemap}
简短回答:get_absolute_url()
应该对对象实施规范URL。
为了生成站点地图。 Django 将在模型对象上调用 .location(…)
method [Django-doc] of the Sitemap
object on each element. If no .location(…)
method is defined, as is the case here. It will call the .get_absolute_url(…)
method [Django-doc]。
您实现了一个 get_absolute_url
方法,但它看起来像:
def get_absolute_url(self):
return <b>reverse('home')</b>
这意味着 Post
对象的所有“详细信息页面”显然都是主页,因此这是站点地图中唯一的页面。它也不是真正满足 .get_absolute_url(…)
方法的条件:
Define a get_absolute_url()
method to tell Django how to calculate the canonical URL for an object. To callers, this method should appear to return a string that can be used to refer to the object over HTTP.
规范的 URL 意味着每个对象通常 唯一 。因此,如果您的路径定义如下:
path('post/<b><int:pk></b>', post_details, name='post_details')
那么通常规范的 url 是:
def get_absolute_url(self):
return <b>reverse('post_details', kwargs={'pk': self.pk})</b>
静态部分工作正常,但我的博客应用程序的动态帖子未生成
from django.contrib.sitemaps import Sitemap
from django.urls import reverse
from .models import Post
class PostSitemap(Sitemap):
changefreq = "weekly"
priority = 0.9
def items(self):
return Post.objects.all()
我无法通过我的博客和模型中的 Post class 获取动态站点地图:
class Post(models.Model):
title=models.CharField(max_length=100)
header_image = models.ImageField(null=True , blank=True, upload_to="images/")
title_tag=models.CharField(max_length=100)
author= models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextUploadingField(extra_plugins=
['youtube', 'codesnippet'], external_plugin_resources= [('youtube','/static/ckeditor/youtube/','plugin.js'), ('codesnippet','/static/ckeditor/codesnippet/','plugin.js')])
#body = models.TextField()
post_date = models.DateTimeField(auto_now_add=True)
category = models.CharField(max_length=50, default='uncategorized')
snippet = models.CharField(max_length=200)
likes = models.ManyToManyField(User, blank=True, related_name='blog_posts')
def total_likes(self):
return self.likes.count()
class Meta:
verbose_name = "Entrada"
verbose_name_plural = "Entradas"
ordering = ['-post_date']
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('home')
如果有人能帮助我,我将不胜感激。 urls.py:
from django.contrib.sitemaps.views import sitemap
from theblog.sitemaps import PostSitemap, StaticSitemap
sitemaps = {'static': StaticSitemap, 'blog': PostSitemap}
简短回答:get_absolute_url()
应该对对象实施规范URL。
为了生成站点地图。 Django 将在模型对象上调用 .location(…)
method [Django-doc] of the Sitemap
object on each element. If no .location(…)
method is defined, as is the case here. It will call the .get_absolute_url(…)
method [Django-doc]。
您实现了一个 get_absolute_url
方法,但它看起来像:
def get_absolute_url(self):
return <b>reverse('home')</b>
这意味着 Post
对象的所有“详细信息页面”显然都是主页,因此这是站点地图中唯一的页面。它也不是真正满足 .get_absolute_url(…)
方法的条件:
Define a
get_absolute_url()
method to tell Django how to calculate the canonical URL for an object. To callers, this method should appear to return a string that can be used to refer to the object over HTTP.
规范的 URL 意味着每个对象通常 唯一 。因此,如果您的路径定义如下:
path('post/<b><int:pk></b>', post_details, name='post_details')
那么通常规范的 url 是:
def get_absolute_url(self):
return <b>reverse('post_details', kwargs={'pk': self.pk})</b>