代码拉取完成,页面将自动刷新
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。