欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-10-06 13:20:15
一、新建项目python ->django web 项目 (选择带bootstrap风格与twwriter)项目名称iepiececomputing (ie计件计算)跳出窗体 -> 添加虚拟环境 -(vs2017 自动下载环境 ) 一路确定 安装完后输出You are using pip versi ......

一、新建项目
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