1 Star 0 Fork 0

当时明月/RangoBlog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
populate_rango.py 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
当时明月 提交于 2017-01-06 16:19 . begin again
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_rango.settings")
import django
django.setup()
from blog.models import Category, Page
def populate():
python_pages = [
{"title": "Offical python Tutorial",
"text": "http://docs.python.org/3/tutorial/",
"views": 32},
{"title": "How to Think like a Computer Scientist",
"text": "http://www.greenteapress.com/thinkpython/",
"views": 54},
{"title": "Learn Python in 10 Minutes",
"text": "http://www.korokithaks.net/tutorials/python/",
"views": 77}
]
django_pages = [
{"title": "Official Django Tutorial",
"text": "https://docs.djangoproject.com/en/1.9/intro/tutorial01/",
"views": 15},
{"title": "Django Rocks",
"text": "http://www.djangorocks.com/",
"views": 8},
{"title": "How to Tango with Django",
"text": "http://www.tangowithdjango.com/",
"views": 72}
]
other_pages = [
{"title": "Bottle",
"text": "http://bottlepy.org/docs/dev/",
"views": 23},
{"title": "Flask",
"text": "http://flask.pocoo.org/",
"views": 32}
]
cats = {
# "Python": {"pages": python_pages, "views": 128, "likes": 64},
# "Django": {"pages": django_pages, "views": 64, "likes": 32},
# "Other Frameworks": {"pages": other_pages, "views": 32, "likes": 16}
"Python": {"pages": python_pages, "views": 128},
"Django": {"pages": django_pages, "views": 64},
"Other Frameworks": {"pages": other_pages, "views": 32}
}
for cat, cat_data in cats.items():
c = add_cat(cat, cat_data['views'])
for p in cat_data["pages"]:
add_page(c, p["title"], p["text"], p['views'])
for c in Category.objects.all():
for p in Page.objects.filter(category=c):
print("-{0}-{1}".format(str(c), str(p)))
def add_page(cat, title, text, views=0):
p = Page.objects.get_or_create(category=cat, title=title)[0]
p.text = text
p.views = views
p.save()
return p
def add_cat(name, views, likes):
c = Category.objects.get_or_create(name=name)[0]
c.views = views
# c.likes = likes
c.save()
return c
if __name__ == '__main__':
print("Starting Rango population script...")
populate()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/ppcba/RangoBlog.git
[email protected]:ppcba/RangoBlog.git
ppcba
RangoBlog
RangoBlog
master

搜索帮助