- 限制博客标题的长度,主题和站点名称的字数?防止标题党,只能描述技术或者功能
- 需要调研,应该赋予多长的字符?
- 设计思维:用户信息和个人站点信息是一对一的关系,业务分离【解耦】
- 如何写一篇好博客? ```powershell
- 不存在循环链接引用,引用后无新增描述
- 非英文文档机器翻译,毫无流畅性可言
应该解释语句语法,而非只有一个结果 ```
将博客写作要求放置在网站文章写作页面,重点提示?
为用户上线年度词云,关于文章标签集中度的
# 环境准备
pip install cryptography==3.4.7 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 避免自定义与默认冲突(abstract)
AUTH_USER_MODEL = "blog.UserInfo"
准备数据库 ```sql mysql> show databases; +——————————+ | Database | +——————————+ | information_schema | | mysql | | performance_schema | | sakila | | sys | | test | | world | +——————————+ 7 rows in set (0.01 sec)
mysql> create database blog charset=utf8; Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> create user blogger identified by ‘blog123’; Query OK, 0 rows affected (0.03 sec)
mysql> grant all on blog.* to ‘blogger’@’%’ with grant option; Query OK, 0 rows affected (0.01 sec)
<a name="6LvCZ"></a>
# 数据库字段迁移时遇见的报错
报错内容
```powershell
Traceback (most recent call last):
File "C:\Users\41999\Documents\项目管理\Blog\Scripts\cnblog\manage.py", line 22, in <module>
main()
File "C:\Users\41999\Documents\项目管理\Blog\Scripts\cnblog\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\41999\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\41999\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Users\41999\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\41999\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\41999\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\41999\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\41999\Documents\项目管理\Blog\Scripts\cnblog\blog\models.py", line 81, in <module>
class Article2Tag(models.Model):
File "C:\Users\41999\Documents\项目管理\Blog\Scripts\cnblog\blog\models.py", line 86, in Article2Tag
article = models.ForeignKey(verbose_name='文章', to="Article", to_field='nid', on_delete=True)
File "C:\Users\41999\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\related.py", line 813, in __init__
raise TypeError('on_delete must be callable.')
TypeError: on_delete must be callable.
涉及到的知识点
1. 去官网搜索查找到语法描述[https://docs.djangoproject.com/zh-hans/3.2/ref/models/fields/#model-field-types]
2. 通过查看代码,需要准确判断表关系,由于此表是中间表,有引入新概念
3. 通过中间表来维护Model与Model之间的多对多关系。
4. on_delete描述 [https://docs.djangoproject.com/zh-hans/3.2/ref/models/fields/#model-field-types]
5. 中间表参考文档[https://www.cnblogs.com/lavender1221/p/12499256.html]
6. 反向查询冲突,通过related_name解决 [https://blog.csdn.net/DanielJackZ/article/details/110691546]
学习心得
1. 中间表相当于一个水井,连接两个model,同时可以增加一些自己的内容,在中间表中需要使用对象外键绑定两端
报错以及解决方案
# 报错
ERRORS:
blog.Blog.nid: (fields.E302) Reverse accessor for 'blog.Blog.nid' clashes with field name 'blog.UserInfo.blog'.
HINT: Rename field 'blog.UserInfo.blog', or add/change a related_name argument to the definition for field 'blog.Blog.nid'.
blog.Blog.nid: (fields.E303) Reverse query name for 'blog.Blog.nid' clashes with field name 'blog.UserInfo.blog'.
HINT: Rename field 'blog.UserInfo.blog', or add/change a related_name argument to the definition for field 'blog.Blog.nid'.
System check identified 2 issues (0 silenced).
# 解决方案,分别为两个类的字段设置related_name
实现登录验证
# 配置静态文件目录
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
# 添加模板配置文件
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
包缺失
'cryptography' package is required for sha256_password or caching_sha2_password auth methods
项目结构消失——删除.idea文件重新加载
参考与引用
【https://blog.csdn.net/m0_37296444/article/details/110817300】
报错分析:
1. 配置问题: 模板位置和app位置均已添加
2. 将文件夹作为资源路径