有时候会有这样的需求,为django
执行一些定时任务,比如通知搜索引擎,例如百度,提交网站的一些地址给他们,通过为django
的manage.py
添加自定义命令可以很容易的解决这个问题。下面介绍下如何添加。
首先需要在创建好的应用的根目录创建文件夹名为management
的目录,然后继续在该目录创建commands
的目录,并在两个目录中都要创建__init__.py
的python
文件。
目录创建好之后继续在commands的目录中添加ping_baidu.py
文件,文件名将会是manage.py
的命令名.
目录结构如下:
然后是代码:
from django.core.management.base import BaseCommand, CommandError
from blog.models import Article, Tag, Category
from DjangoBlog.spider_notify import sipder_notify
from django.contrib.sites.models import Site
site = Site.objects.get_current().domain
class Command(BaseCommand):
help = 'notify baidu url'
def add_arguments(self, parser):
parser.add_argument('data_type', type=str, choices=['all', 'article', 'tag', 'category'],
help='article : all article,tag : all tag,category: all category,all: All of these')
def get_full_url(self, path):
url = "https://{site}{path}".format(site=site, path=path)
return url
def handle(self, *args, **options):
type = options['data_type']
self.stdout.write('start get %s' % type)
notify = sipder_notify()
urls = []
if type == 'article' or type == 'all':
for article in Article.objects.filter(status='p'):
urls.append(article.get_full_url())
if type == 'tag' or type == 'all':
for tag in Tag.objects.all():
url = tag.get_absolute_url()
urls.append(self.get_full_url(url))
if type == 'category' or type == 'all':
for category in Category.objects.all():
url = category.get_absolute_url()
urls.append(self.get_full_url(url))
self.stdout.write(self.style.SUCCESS('start notify %d urls' % len(urls)))
notify.baidu_notify(urls)
self.stdout.write(self.style.SUCCESS('finish notify'))
sipder_notify也很简单:
from django.contrib.sitemaps import ping_google
import requests
from django.conf import settings
class sipder_notify():
def baidu_notify(self, urls):
try:
data = '\n'.join(urls)
result = requests.post(settings.BAIDU_NOTIFY_URL, data=data)
print(result.text)
except Exception as e:
print(e)
def __google_notify(self):
try:
ping_google('/sitemap.xml')
except Exception as e:
print(e)
def notify(self, url):
self.baidu_notify(url)
self.__google_notify()
至此,基本都完成了,可以终端执行./manage.py
查看输出:
可以看到 ping_baidu
命令已经出现了,./manage.py ping_baidu --help
可以查看帮助:
执行也很简单,终端执行:
./manage.py ping_baidu all
markdown支持图片嘛?
厉害呀大佬
厉害啦
非常感谢~~ 另外测试的话请移步 测试 来测试.谢谢.
为什么你的登陆系统,登陆了微博还要继续提交邮箱地址? 用微博登陆不就行了么,为什么要继续用自己的登陆再注册一次
测试一下评论有没有树结构
继续测试,树结构评论
因为你的微博没有邮箱地址~~后续的评论回复等无法通知到你~~
学习了