Django3.0+Python3.8+MySQL8.0 个人博客搭建五|makemigrations连接MySQL数据库的坑
Django3.0+Python3.8+MySQL8.0 个人博客搭建七|makemigrations创建数据库的坑(第二弹)
django-haystack
全文检索的坑HAYSTACK
配置问题django.core.exceptions.ImproperlyConfigured: The HAYSTACK_CONNECTIONS setting is required.
未配置HAYSTACK
在settings.py
中
# 全文搜索应用配置
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'fswy.whoosh_cn_backend.WhooshEngine', # 选择语言解析器为自己更换的结巴分词
'PATH': os.path.join(BASE_DIR, 'whoosh_index'), # 保存索引文件的地址,选择主目录下,这个会自动生成
}
}
# 自动更新索引
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
django-haystack全文检索详细教程
Haystack入门教程
six
模块找不到ImportError: cannot import name 'six' from 'django.utils'
The Django 3.0.0 release notes specify that certain private Python 2
compatibility APIs were removed. Among those was django.utils.six.
Django 3.0.0
发布说明指定删除了某些专用的Python 2兼容性api。其中包括django.utils.six
。
$ pip3 install six
/Users/xiatian/fswy/blog/venv/lib/python3.8/site-packages
six.py
复制到 /Users/xiatian/fswy/blog/venv/lib/python3.8/site-packages/django/utils
即可Django测试开发-16-ImportError: cannot import name ‘six’ from ‘django.utils’
ImportError: cannot import name ‘six’ from ‘django.utils’
python_2_unicode_compatible
在django.utils.encoding
无法找到File "/Users/xiatian/fswy/blog/venv/lib/python3.8/site-packages/haystack/inputs.py", line 8, in <module>
from django.utils.encoding import force_text, python_2_unicode_compatible
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding' (/Users/xiatian/fswy/blog/venv/lib/python3.8/site-packages/django/utils/encoding.py)
还是six
在django3.0中被移除的问题
进入/Users/xiatian/fswy/blog/venv/lib/python3.8/site-packages/haystack/inputs.py
文件中
请使用
from six import python_2_unicode_compatible
替代
from django.utils.encoding import python_2_unicode_compatible
/Users/xiatian/fswy/blog/apps/fswy/whoosh_cn_backend.py:618: SyntaxWarning: "is" with a literal. Did you mean "=="?
新增危险语法警告功能
Python有一个SyntaxWarning
功能,可以警告不是SyntaxError
的可疑语法。
Python 3.8
添加了一些新功能,可以在编码和调试过程中为你提供帮助。
is
和==
之间的区别可能会造成混淆。后者用于检查是否有相等的值,而只有在对象相同时才为true
。
Python 3.8
将在应该使用==
而不是is
时发出警告:
>> # Python 3.7
>> version = "3.7"
>> version is "3.7"
False
>> # Python 3.8
>> version = "3.8"
>> version is "3.8"
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
>> version == "3.8"
True
进入
/Users/xiatian/fswy/blog/apps/fswy/whoosh_cn_backend.py
将第618行代码
if value is None or len(value) is 0:
改为
if value == None or len(value) == 0:
raise RuntimeError("cryptography is required for sha256_password or caching_sha2_password")
Pycharm连接数据库出现
安装cryptography
$ pip3 install cryptography
RuntimeError: cryptography is required for sha256_password or caching_sha2_password
haystack
ModuleNotFoundError: No module named 'haystack'
这个问题搞了我好久,因为最开始是直接在PyCharm中直接安装的django-haystack
,但是在终端$ python3 manage.py rebuild_index
建立索引的时候,一直报这个错误,硬是没找到思路。
找了半天解决方案,
第一种:
$ pip3 install haystack
再运行rebuild_index
ImportError: cannot import name 'connections' from 'haystack' (/Users/xiatian/fswy/lib/python3.8/site-packages/haystack/__init__.py)
haystack
和django-haystack
,这两个包有冲突.
网上的方法都只是删除了haystack
,但还是报错
我的解决方法是:
pip uninstall haystack
pip uninstall django-haystack
pip install django-haystack
ImportError: cannot import name ‘connections’ from ‘haystack’
再运行rebuild_index
raise MissingDependency("The 'whoosh' backend requires the installation of 'Whoosh'. Please refer to the documentation.")
这里很简单就是没有安装whoosh
$ pip3 install whoosh
再运行rebuild_index
(fswy) blog xiatian$ python3 manage.py rebuild_index
WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.
Are you sure you wish to continue? [y/N] y
Removing all documents from your index because you said so.
All documents removed.
Indexing 2 文章
Building prefix dict from the default dictionary ...
Loading model from cache /var/folders/7d/s7t1kh7n4x59zqltw7gs26640000gn/T/jieba.cache
Loading model cost 0.745 seconds.
Prefix dict has been built successfully.
总结一下,就是要在终端中安装django-haystack!
File "/Users/xiatian/fswy/blog/blog/urls.py", line 24, in <module>
path('', include('fswy.urls', namespace='blog'))
File "/Users/xiatian/fswy/blog/venv/lib/python3.8/site-packages/django/urls/conf.py", line 38, in include
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
查找错误出处,为conf.py
文件
源代码如下:
def include(arg, namespace=None):
app_name = None
if isinstance(arg, tuple):
# Callable returning a namespace hint.
try:
urlconf_module, app_name = arg
except ValueError:
if namespace:
raise ImproperlyConfigured(
'Cannot override the namespace for a dynamic module that '
'provides a namespace.'
)
raise ImproperlyConfigured(
'Passing a %d-tuple to include() is not supported. Pass a '
'2-tuple containing the list of patterns and app_name, and '
'provide the namespace argument to include() instead.' % len(arg)
)
else:
# No namespace hint - use manually provided namespace.
urlconf_module = arg
if isinstance(urlconf_module, str):
urlconf_module = import_module(urlconf_module)
patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
app_name = getattr(urlconf_module, 'app_name', app_name)
if namespace and not app_name:
raise ImproperlyConfigured(
'Specifying a namespace in include() without providing an app_name '
'is not supported. Set the app_name attribute in the included '
'module, or pass a 2-tuple containing the list of patterns and '
'app_name instead.',
)
namespace = namespace or app_name
# Make sure the patterns can be iterated through (without this, some
# testcases will break).
if isinstance(patterns, (list, tuple)):
for url_pattern in patterns:
pattern = getattr(url_pattern, 'pattern', None)
if isinstance(pattern, LocalePrefixPattern):
raise ImproperlyConfigured(
'Using i18n_patterns in an included URLconf is not allowed.'
)
从include()
函数可以看出来,这个函数有两个参数,一个arg
,一个namespace
,我在代码中也是两个参数,但是异常中提示了,没有提供app_name,还提示需要传入一个两元元组,从第六行代码
urlconf_module, app_name = arg
可以看出来,arg就是那个元组,且给app_name赋值了
path('', include('fswy.urls', namespace='blog'))
改为
path('', include(('fswy.urls', "fswy"), namespace="blog")),
Django2.0异常:Specifying a namespace in include() without providing an app_name is not supported.
'staticfiles' is not a registered tag library. Must be one of: admin_list admin_modify admin_urls cache i18n imagekit l10n log static tz
{% load staticfiles %}
和{% load adminstatic %}
已经在Django 2.1
和Django 3.0
中移除。
如果你要在你的HTML页面中用下面的代码
{% load staticfiles %}
{% load static from staticfiles %}
{% load adminstatic %}
你必须用下面的代码代替(仅所有.html文件)
{% load static %}
TemplateSyntaxError - ‘staticfiles’ is not a registered tag library. Must be one of:
Error: That port is already in use.
8000端口已被占用
有两个方法可以解决(假设8000端口被占用):
使用python manage.py runserver 8001
开一个新的端口。
lsof -i:8000
,列出进程信息。kill -9 PID
,比如kill -9 92934
就可以关闭该端口了。python manage.py runserver
就能继续使用8000端口了。解决Django-Error: That port is already in use
Invaild block tag on line 12:'xxxxx',expected 'endblock'. Did you forget to register or load this tag?
这类问题都基本是’xxxxx’方法写错了,或者是引用出问题
Django3.0+Python3.8+MySQL8.0 个人博客搭建一|前言
Django3.0+Python3.8+MySQL8.0 个人博客搭建二|创建虚拟环境
Django3.0+Python3.8+MySQL8.0 个人博客搭建三|创建博客项目
Django3.0+Python3.8+MySQL8.0 个人博客搭建四|创建第一个APP
Django3.0+Python3.8+MySQL8.0 个人博客搭建五|makemigrations连接MySQL数据库的坑
Django3.0+Python3.8+MySQL8.0 个人博客搭建六|数据库结构设计
Django3.0+Python3.8+MySQL8.0 个人博客搭建七|makemigrations创建数据库的坑(第二弹)
Django3.0+Python3.8+MySQL8.0 个人博客搭建八|通过admin管理后台
Django3.0+Python3.8+MySQL8.0 个人博客搭建九|博客首页开发(一)
Django3.0+Python3.8+MySQL8.0 个人博客搭建十|整理项目结构
Django3.0+Python3.8+MySQL8.0 个人博客搭建十一|博客首页开发(二)
Django3.0+Python3.8+MySQL8.0 个人博客搭建十二|博客首页开发(三)
Django3.0+Python3.8+MySQL8.0 个人博客搭建十三|博客详情页面
Django3.0+Python3.8+MySQL8.0 个人博客搭建十四|注册登录
Django3.0+Python3.8+MySQL8.0 个人博客搭建十五|评论区
Django3.0+Python3.8+MySQL8.0 个人博客搭建十六|网站地图
Django3.0+Python3.8+MySQL8.0 个人博客搭建十七|Haystack 全文搜索
Django3.0+Python3.8+MySQL8.0 个人博客搭建十八|功能完善
Django3.0+Python3.8+MySQL8.0 个人博客搭建十九|RESTful API接口
Django3.0+Python3.8+MySQL8.0 个人博客搭建二十|RSS订阅