Django 工程搭建
新建虚拟环境
mkvirtualenv --python=/usr/local/bin/python3 blogdjango
安装Django rest framework
进入blogdjango虚拟环境, Django-Rest-Framework
pip install djangorestframework
pip install markdown
pip install django-filter
pip install django
配置pycharm工程
- project interpreter 选择上面新建的blogdjango虚拟环境,其他选项默认
- more settings里面的application name输入users,因为基本每个django工程都会有users这个application。不启用Django admin,因为我们使用xadmin
- 修改settings.py文件,修改数据库连接串:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blogdjango',
'USER': 'root',
'PASSWORD': 'Flask123.',
'HOST': '127.0.0.1',
'OPTIONS': {'init_command': 'SET default_storage_engine=INNODB;'}
}
}
试运行项目,但是会报错,需要安装pymysql和mysqlclient
pip install mysqlclient
pip install pymysql
- No module named 'MySQLdb'
原因:python3连接mysql不能再使用mysqldb,要是用pymysql
方法:apps的__init__.py下添加如下语句
import pymysql
pymysql.install_as_MySQLdb()
- django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
原因:直接取了pymysql的版本没有取mysqlclient
方法:在/Users/liyihao/PycharmProjects/blogdjango/venv/lib/python3.6/site-packages/django/db/backends/mysql/base.py 注释掉如下语句
if version < (1, 3, 13):
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
- AttributeError: 'str' object has no attribute 'decode'
原因:函数用错了
方法:在/Users/liyihao/PycharmProjects/blogdjango/venv/lib/python3.6/site-packages/django/db/backends/mysql/operations.py文件下的decode修改修改成encode
query = getattr(cursor, '_executed', None)
if query is not None:
query = query.decode(errors='replace')
return query
-
收集static文件
python manage.py collectstatic
网友评论