小试VS 2017 开发Python Django项目过程一

一、新建项目
python ->django web 项目 (选择带bootstrap风格与twwriter)
项目名称
iepiececomputing (ie计件计算)
跳出窗体 -> 添加虚拟环境 -(vs2017 自动下载环境 )

一路确定

安装完后输出
You are using pip version 18.1, however version 19.0.3 is available.
—– 已成功安装“-r D:\code\IEPieceWorkComputin\IEPieceComputin\requirements.txt” —–

“requirements.txt”已成功安装。

 

A、安装pymysql
点击 python环境下evn节点,右键,选择安装python包
输入pymysql
点击下方出现的 pip install pymysql命令既可
可参见
https://www.cnblogs.com/wortliu/p/9237753.html

 

B、在iepiececomputing目录下找到 settings.py

将database改为

DATABASES = {
#’default’: {
# ‘ENGINE’: ‘django.db.backends.sqlite3’,
# ‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’),
#}
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘HOST’: ‘127.0.0.1’,
‘PORT’: ‘3306’,
‘NAME’: ‘databasename’,
‘USER’: ‘root’,
‘PASSWORD’: ‘zjshizhu123’,
}
}

INSTALLED_APPS 下增加

‘IEPieceComputing’,

C、在iepiececomputing目录下找到 __init__.py (在与项目同名包的__init__下添加)
添加代码
import pymysql
pymysql.install_as_MySQLdb()

 

D、在 环境 env(python 3.7 64bit) 上点右键 选择在文件管理器中打开 然后打开
Lib\site-packages\django\db\backends\mysql目录 最终地址
D:\code\IEPieceComputing\IEPieceComputing\env\Lib\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__)

 

找到operation.py 146 147行注解掉
#if query is not None:
#  query = query.decode(errors=’replace’)
python3以上版本没有decode了..

E、添加数据模型
右键点击解决方案,选择在此处打开命令提示行,主要是看manage.py文件在哪个目录下

1、如果已经在MySql中建好了表使用
python manage.py inspectdb

2、如果已经在App/models.py中建好了表使用
python manage.py migrate

3、忘记了命令
python manage.py help

本次在MySQL中已经建好一个表叫DataCommon
同时又在app/models.py里面建了一个模型叫
class Employee(models.Model):
name=models.CharField(max_length=20)同时测试1与2。结果还是满意的

下面是执行顺序
D:\code\IEPieceComputing\IEPieceComputing>python manage.py makemigrations
Migrations for ‘app’:
app\migrations\0001_initial.py
– Create model Datacommon
– Create model Employee

D:\code\IEPieceComputing\IEPieceComputing>python manage.py migrate
Operations to perform:
Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying admin.0003_logentry_add_action_flag_choices… OK
Applying app.0001_initial… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying auth.0009_alter_user_last_name_max_length… OK
Applying auth.0010_alter_group_name_max_length… OK
Applying auth.0011_update_proxy_permissions… OK
Applying sessions.0001_initial… OK

D:\code\IEPieceComputing\IEPieceComputing>python manage.py createsuperuser
用户名 (leave blank to use ‘riland.0605’): riland
电子邮件地址: abcde@qq.com
Password:
Password (again):
密码长度太短。密码必须包含至少 8 个字符。
这个密码太常见了。
密码只包含数字。
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

备注,进行上述操作的时候,出现了三个杂音(错误或是问题),每一次都不顺利

第一个, python manage.py migrate 提示
File “D:\code\IEPieceComputing\IEPieceComputing\env\lib\site-packages\django\db\backends\mysql\operations.py”, line 146, in last_executed_query
query = query.decode(errors=’replace’)
AttributeError: ‘str’ object has no attribute ‘decode’
原因为pymysql模块比较老,decode已经在python3中没有该方法了
按上述操作直接注解掉
解决完了又提示第二个

第二个python manage.py migrate 提示
#?: (admin.E408) ‘django.contrib.auth.middleware.AuthenticationMiddleware’ must be in MIDDLEWARE in order to use the admin application.
#?: (admin.E409) ‘django.contrib.messages.middleware.MessageMiddleware’ must be in MIDDLEWARE in order to use the admin application.
#
看了一下英文文档在

https://docs.djangoproject.com/en/1.11/ref/contrib/admin/
visual studio 2017在settings.py 生成的变量名是
MIDDLEWARE_CLASSES
而要求的变量名为MIDDLEWARE
直接拷一份,重命名。就好了

MIDDLEWARE = [
‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’,
‘django.contrib.auth.middleware.SessionAuthenticationMiddleware’,
]

 

第三个,提示django.contrib.auth.views.login等views 不存在
直接在urls.py中注解掉,不用再意,这也是vs2017提示了 # Examples: ….意思就是照着这个样子写..

E、启用Admin模块,上述建好了表与用户下面就直接启用admin模块,
找到urls.py 将此行
# url(r’^admin/’, include(admin.site.urls)),
前的#去掉即可
此处也有比较详细的讲解
https://code.ziqiangxuetang.com/django/django-admin.html

 

点赞