美文网首页
20230105-快速创建Django项目

20230105-快速创建Django项目

作者: 負笈在线 | 来源:发表于2023-01-04 22:27 被阅读0次

一、Python Install(兼容python2/python3)

python3(python-3.6.8-amd64.exe) install
--D:\Python36
python2(python-2.7.16.amd64.msi) install
--D:\Python27
参考URL:
https://blog.csdn.net/zunguitiancheng/article/details/126762742

二、python2/python3及pip2/pip3共存问题解决

参考URL:
https://www.cnblogs.com/MoZiYa/p/16690252.html

C:\Users\Administrator>python2
Python 2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> exit()
>>> 
C:\Users\Administrator>python3
>>> Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
>>> Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\Users\Administrator>pip2 -V
>>> pip 20.3.4 from d:\python27\lib\site-packages\pip (python 2.7)

C:\Users\Administrator>pip3 -V
pip 21.3.1 from d:\python36\lib\site-packages\pip (python 3.6)

三、pip源(国内)配置

新建配置文件C:\Users\Administrator\pip\pip.ini,编辑内容如下:

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com

常见国内镜像源
(1)阿里云 http://mirrors.aliyun.com/pypi/simple/
(2)豆瓣http://pypi.douban.com/simple/
(3)清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
(4)中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
(5)华中科技大学http://pypi.hustunique.com/

四、虚拟环境安装

C:\Users\Administrator>pip3 install virtualenv
C:\Users\Administrator>pip3 install virtualenvwrapper-win
C:\Users\Administrator>pip2 install virtualenv
C:\Users\Administrator>pip2 install virtualenvwrapper-win

设置系统变量 WORKON_HOME
参考URL:
https://wenku.baidu.com/view/86da82f92bea81c758f5f61fb7360b4c2e3f2adb.html?wkts=1672899702137&bdQuery=windows+mkvirtualenvwrapper

命令:创建虚拟环境

mkvirtualenv test
mkvirtualenv  -p python2 test2
mkvirtualenv  -p python3 test2

命令:列出虚拟环境

workon

命令:进入虚拟环境

workon test
pip list
python

命令:退出虚拟环境

deactivate

命令:删除虚拟环境

rmvirtualenv test

五、创建Django项目虚拟环境

C:\Users\Administrator>mkvirtualenv -p python3 local_cmdb
(local_cmdb) C:\Users\Administrator>workon local_cmdb
(local_cmdb) C:\Users\Administrator>pip -V           

六、安装Django:

(local_cmdb) C:\Users\Administrator>cd D:\python_work\local_cmdb
(local_cmdb) C:\Users\Administrator>D:
(local_cmdb) D:\python_work\local_cmdb>django-admin startproject local_cmdb
(local_cmdb) D:\python_work\local_cmdb>cd local_cmdb
(local_cmdb) D:\python_work\local_cmdb\local_cmdb>python3 manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 05, 2023 - 15:28:46
Django version 3.2.16, using settings 'local_cmdb.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CTRL-BREAK.

访问http://127.0.0.1:8000/

七、MYSQL安装配置

参考URL:
https://baijiahao.baidu.com/s?id=1734145282045952263&wfr=spider&for=pc
https://downloads.mysql.com/archives/installer/

MYSQL_VERSION:5.7.39

C:\Users\Administrator>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 57
Server version: 5.7.39-log MySQL Community Server (GPL)
mysql> create database local_cmdb;
Query OK, 1 row affected (0.00 sec)

mysql>

修改默认的sqlite数据库为mysql

(local_cmdb) D:\python_work\local_cmdb\local_cmdb>pip3 install mysqlclient==2.0.0
(local_cmdb) D:\python_work\local_cmdb\local_cmdb>python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, 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 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 auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

(local_cmdb) D:\python_work\local_cmdb\local_cmdb>python3 manage.py createsuperuser
Username (leave blank to use 'administrator'): luorf
Email address: luorunfan@qq.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

再启动服务

(local_cmdb) D:\python_work\local_cmdb\local_cmdb>python3 manage.py runserver 0.0.0.0:8000

访问http://127.0.0.1:8000/admin/

八、整合simpleui模板

(local_cmdb) D:\python_work\local_cmdb\local_cmdb>pip3 install simpleui

修改配置文件 settings.py

import os        ##追加1
from pathlib import Path

INSTALLED_APPS = [
    'simpleui',                              #追加2
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")            #追加3

项目目录下 新建目录: static

(local_cmdb) D:\python_work\local_cmdb\local_cmdb>dir
 Volume in drive D has no label.
 Volume Serial Number is 000A-D386

 Directory of D:\python_work\local_cmdb\local_cmdb

2023/01/05  17:22    <DIR>          .
2023/01/05  17:22    <DIR>          ..
2023/01/05  15:28                 0 db.sqlite3
2023/01/05  17:34    <DIR>          local_cmdb
2023/01/05  15:27               688 manage.py
2023/01/05  17:43    <DIR>          static
               2 File(s)            688 bytes
               4 Dir(s)   6,932,615,168 bytes free

克隆资源:

(local_cmdb) D:\python_work\local_cmdb\local_cmdb>python3 manage.py collectstatic

重启服务

(local_cmdb) D:\python_work\local_cmdb\local_cmdb>python3 manage.py runserver

访问http://127.0.0.1:8000/admin/

相关文章

网友评论

      本文标题:20230105-快速创建Django项目

      本文链接:https://www.haomeiwen.com/subject/zgsdcdtx.html