django虚拟环境新建项目

CMD命令行:

C:\Users\Administrator>mkvirtualenv mxonline  (新建mxonline(自定义名)虚拟环境,新建虚拟环境传送门:点这里

(mxonline) C:\Users\Administrator>pip install django==1.9.8  (安装django)


pycharm新建项目工程:

名称:MxOnline

解释器:C:\Users\Administrator\Envs\mxonline\Scripts\python.exe



安装mysql驱动:


Command "c:\users\administrator\envs\mxonline\scripts\python.exe -u -c "import s
etuptools, tokenize;__file__='c:\\users\\admini~1\\appdata\\local\\temp\\pip-ins
tall-ua6lix\\mysql-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__)
;code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exe
c'))" install --record c:\users\admini~1\appdata\local\temp\pip-record-ueirpb\in
stall-record.txt --single-version-externally-managed --compile --install-headers
 c:\users\administrator\envs\mxonline\include\site\python2.7\mysql-python" faile
d with error code 1 in c:\users\admini~1\appdata\local\temp\pip-install-ua6lix\m
ysql-python\

(安装出错,我们之前已经学过;解决方法是直接下载mysql-python的whl文件直接运行安装,下载地址:传送门,下载对应的mysql-python即可)

CMD命令行:

(mxonline) C:\Users\Administrator>cd Downloads  (我的存放地址)

(mxonline) C:\Users\Administrator\Downloads>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl(运行安装)


【配置settings.py】

数据库配置信息≈78行:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mxonline',
        'USER': 'root',
        'PASSWORD':'',
        'HOST': '127.0.0.1'
    }
}

(同样的,我们还需要建立名称为mxonline的数据库):



配置pycharm解释器:

pycharm  菜单栏 >>> File >>> Settings >>> Project Interpreter

1.配置解释器时有可能会出现错误,检查是否配置,如果配置时出现No python interpreter configured for the project

表示没有配置解释器


2.配置虚拟环境的解释器出现:Cannot Save Settings: Please specify a different SDK name时

表示你配置的解释器中可能有重复的,需要删除掉没用的,删除方法如下:



之后就可以正常配置解释器了。


【创建数据表】:

pycharm  菜单栏 >>> Tools >>> Run manage.py Task >>> (创建django的默认数据表)

如果打开Run manage.py task出现以下错误时:

Error fetching command 'collectstatic': You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
Command 'collectstatic' skipped

可在 settings.py中添加:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')


manage.py@MxOnline> makemigrations [appname]   (检测生成Django的默认数据表)

出现:No changes detected  我们就可以继续下一步


manage.py@MxOnline> migrate [appname]   (写入数据表)

会显示一系列OK即代表完成。


现在可以Run或者Debug测试一下是否成功访问 http://127.0.0.1:8000

我们还可以设置一下局域网的访问:

pycharm 菜单栏 >>> Run >>> Edit Configurations...  >>> Host >>> 0.0.0.0 

然后CMD命令行  ipconfig 查看本机IP,看到本机IP后即可在局域网中的其他主机中输入此IP:8000来访问Django项目网站



猜你喜欢

转载自blog.csdn.net/qq_40134903/article/details/81061965