修改后台应用名称
进入users->apps.py,UsersConfig加上verbose_name
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'apps.users'
verbose_name = '用户管理'
但发现修改完后,后台应用名称没变化,根本没调用UsersConfig而且name设置有问题,将UsersConfig的name改为 name='users'
,并且在users/__init__py加上
default_app_config = "users.apps.UsersConfig"
这样改完就ok了,接下来把其他的后台应用名称按照users的写法都改完之后,ctrl f5强刷页面
编写用户数据文件
apps文件下新建dbtools包,在dbtools下新建import_users_data.py文件用来存放用户数据,写入
# -*- coding: utf-8 -*-
# 独立使用django的model
import sys
import os
pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + "../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "LehuXuexi.settings")
import django
django.setup()
# 用户数据
users_data = [
{
'username': 'admin',
'nickname': '管理员',
'mobile': '15002959010',
'email': 'admin@lehu.com',
"avatar": "users/avatars/0.jpg",
"is_superuser": True,
"is_staff": True,
"is_active": True
},
{
'username': 'weixiaobao',
'nickname': '韦小宝',
'mobile': '15002959011',
'email': 'weixiaobao@lehu.com',
"avatar": "users/avatars/1.jpg",
"is_superuser": False,
"is_staff": False,
"is_active": True
},
{
'username': 'zhaochunhua',
'nickname': '赵春花',
'mobile': '15002959012',
'email': 'zhaochunhua@lehu.com',
"avatar": "users/avatars/2.jpg",
"is_superuser": False,
"is_staff": False,
"is_active": True
},
{
'username': 'yangliuzhi',
'nickname': '杨柳枝',
'mobile': '15002959013',
'email': 'yangliuzhi@lehu.com',
"avatar": "users/avatars/3.jpg",
"is_superuser": False,
"is_staff": False,
"is_active": True
},
{
'username': 'zhangxiaotian',
'nickname': '张笑天',
'mobile': '15002959014',
'email': 'zhangxiaotian@lehu.com',
"avatar": "users/avatars/4.jpg",
"is_superuser": False,
"is_staff": False,
"is_active": True
},
{
'username': 'yuanhaowen',
'nickname': '元好问',
'mobile': '15002959015',
'email': 'yuanhaowen@lehu.com',
"avatar": "users/avatars/5.jpg",
"is_superuser": False,
"is_staff": False,
"is_active": True
},
]
from users.models import UserProfile
from django.contrib.auth.hashers import make_password
for item in users_data:
if not UserProfile.objects.filter(username=item['username']).first():
user = UserProfile(**item)
#密码hash
user.password = make_password('qaz@wsx.123')
user.save()
print(user.id, '---', user.password)
else:
print(item['username'], '用户名已经存在了!')
运行import_user_data.py,输出
admin 用户名已经存在了!
2 --- pbkdf2_sha256$150000$YPykz4ceKGkR$m3bSPmhMy9Kgi9o2aWobFJ3sbQ5+on0bGx37mK6VN/Q=
3 --- pbkdf2_sha256$150000$DQkxQMICapyK$2zsX/T7V4oUzplLrB/2euBuY5nBdyWJRH3rySSzlMD8=
4 --- pbkdf2_sha256$150000$zLgETn6uZJ7f$J7+wCgZs1EUv+1sHnc22CaVRzVURwonjYLdNfwoJU0c=
5 --- pbkdf2_sha256$150000$O4Yq2P5Jr6PI$v/VKKhzKiM145EubScUE649ZdaBQjHi5zARez28wH7g=
6 --- pbkdf2_sha256$150000$ZCoHexRYhziW$ooe5kSP3idI84faXNgrbwSl+8WBou7FmxLtsGRWmwCc=
此时进入管理平台已经生成了用户数据
编写博客数据文件
在dbtools下新建import_blogs_data.py文件用来执行导入博客数据,在执行前还需要下载事先准备好的文章标题、封面、内容数据,下载地址,将article_titles.txt、articles1-richeditor.html、articles2-richeditor.html放到dbtools下,blogs/articles/covers/*.jpg新增并放到media下,再执行import_blogs_data.py
# -*- coding: utf-8 -*-
# 独立使用django的model
from random import randint, choice
import sys
import os
pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + "../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "LehuXuexi.settings")
import django
django.setup()
from blogs.models import BlogCategory, BlogTag, Article
from django.contrib.auth import get_user_model
User = get_user_model()
# 标签数据
tags_data = ['python', 'java', 'nodejs', 'javascript', 'html', 'css', 'julia', 'matlab', 'go', 'c++', 'linux', 'mysql',
'mongodb', 'vue', 'react', 'django', 'flask', 'tornado', 'latex', 'word', 'pyramid', 'excel', 'bigdata']
for tag_name in tags_data:
if not BlogTag.objects.filter(name=tag_name).first():
tag = BlogTag()
tag.name = tag_name
tag.save()
print(tag.id, ' ---> ', tag.name)
else:
print(tag_name, '已经存在')
# 导入博客类别数据
categories_data = ['Web开发', '人工智能', '天文地理', '环球政治', '人文社科', '动物世界', '道德观察', '财税金融']
for ctg_name in categories_data:
if not BlogCategory.objects.filter(name=ctg_name).first():
category = BlogCategory()
category.name = ctg_name
category.save()
print(category.id, ' ---> ', category.name)
else:
print(ctg_name, '已经存在')
# 导入文章数据
article_content = []
with open('./articles1-richeditor.html', 'r', encoding='UTF-8') as f:
article_content.append(f.read())
with open('./articles2-richeditor.html', 'r', encoding='UTF-8') as f:
article_content.append(f.read())
print(article_content)
article_titles = []
with open('./article_titles.txt', 'rb') as f:
lines = f.readlines()
for item in lines:
article_titles.append(item.decode('utf8').rstrip(',\r\n'))
print(article_titles)
print(len(article_titles))
loop = 0
for title in article_titles:
existed = Article.objects.filter(title=title).first()
if existed:
existed.content = choice(article_content)
existed.save()
else:
loop += 1
article = Article()
article.title = title
article.brief = "别人当他敌人,他却又当敌人是朋友,他永远坚持自己的信条,但在处理具体事物时,又不乏灵活的手腕。"
article.content = choice(article_content)
article.click_num = randint(10, 100)
article.favor_num = randint(10, 100)
article.comment_num = randint(10, 100)
article.category = choice(BlogCategory.objects.all())
article.user = choice(User.objects.all())
article.cover = "blogs/articles/covers/" + str(loop % 40 + 1) + '.jpg'
article.save()
print(article.id)
一共执行69篇博客,后台更新查看
版权声明:如无特殊说明,文章均为本站原创,转载请注明出处
本文链接:http://zhangyanc.club/subject/article/django-userdata/
许可协议:署名-非商业性使用 4.0 国际许可协议