使用Docker时遇到的sitemap问题

点击量:9

这两天感觉博客访问量已经不能靠用嘴宣传了,于是决定做一下sitemap,但是发现django自带的sitemap生成器存在这样的问题:生成的sitemap是依照container的hostname来显示的。

为此我查询了一下其他人是怎么搞的,发现他们都把nginx和django放在一个容器下——但是如此构建不仅麻烦,似乎违背了docker的构建思路,在一个container里面放了多个进程。因此,我认为下面的方案是合理的。

解决方案

Enabling the sites framework

To enable the sites framework, follow these steps:

Add ‘django.contrib.sites’ to your INSTALLED_APPS setting.

Define a SITE_ID setting:

SITE_ID = 1

Run migrate.

django.contrib.sites registers a post_migrate signal handler which creates a default site named example.com with the domain example.com. This site will also be created after Django creates the test database. To set the correct name and domain for your project, you can use a data migration.

In order to serve different sites in production, you’d create a separate settings file with each SITE_ID (perhaps importing from a common settings file to avoid duplicating shared settings) and then specify the appropriate DJANGO_SETTINGS_MODULE for each site.

>>> from django.contrib.sites.models import Site
>>> one = Site.objects.all()[0]
>>> one.domain = 'myveryspecialdomain.com'
>>> one.name = 'My Special Site Name'
>>> one.save()

代码写多了,对于Flask和Django也就十分释然了。两个东西都是利器,针对的东西不是很相同;编程语言也是,都有自己擅长的部分,如果你非要用某种编程语言花很大的力气去做一件小事,除非你是geek而且有追求,并且没有deadline在旁边催催催,否则不建议你这么做。

参考项目

https://github.com/flyhigher139/mayblog

发表评论

电子邮件地址不会被公开。 必填项已用*标注