您好,欢迎访问一九零五行业门户网

Django遇到的地雷,大家要注意

最近在折腾django的wsgi应用,虽然django自带的runserver很方便,但是对于复杂的功能它就无能为力了。
首先在windows上遇到坑了,然后在windows10自带的ubuntu遇到坑了,最后在虚拟机上总算解决了。
从以前的文章django笔记《django 学习笔记(二)第一个网页》可以看出我用的是windows10系统中的cmd来进行django操作的,由于gunicorn应用是在unix系统中运行的,强制性在win平台运行就出错了。
1.windows遇到坑:
首先 cmd中输入:pip3 install gunicorn ,安装成功后,切换到项目目录(g:/django/hello),具体操作是先切换到g盘,直接输入g:,接着输入cd django\hello 就会出现 g:\django\hello>这种情况。然后运行gunicorn,在django项目中格式是:gunicorn yourproject.wsgi [-b 127.0.0.1.8000],括号内为选填,其他命令参数可以发送 gunicorn -h得到帮助文档。我这边的项目是hello,所以我的命令操作是:gunicorn hello.wsgi ,然后呢,出错modulenotfounderror: no module named 'pwd',没有pwd模块,好吧,没有找呗。找到后的代码贴在下面了,把找到的pwd.py放进了python3的安装目录d:\program files\python\python36\lib,接着我们再运行gunicorn hello.wsgi ,结果又出错了:attributeerror: module 'socket' has no attribute 'af_unix' ,然后我又在网上找,因为是unix系统中的gunicorn,结果找不到liunx中的sock.py文件,所以转战unix系统,windows弃坑。
g:\django\hello>gunicorn hello.wsgi traceback (most recent call last):   file d:\program files\python\python36\lib\runpy.py, line 193, in _run_module_as_main__main__, mod_spec)   file d:\program files\python\python36\lib\runpy.py, line 85, in _run_codeexec(code, run_globals)   file d:\program files\python\python36\scripts\gunicorn.exe\__main__.py, line 5, in <module>   file d:\program files\python\python36\lib\site-packages\gunicorn\app\wsgiapp.py, line 10, in <module>from gunicorn.app.base import application   file d:\program files\python\python36\lib\site-packages\gunicorn\app\base.py, line 12, in <module>from gunicorn import util   file d:\program files\python\python36\lib\site-packages\gunicorn\util.py, line 13, in <module>import pwd modulenotfounderror: no module named 'pwd'
modulenotfounderror: no module named 'pwd'
from os import *  from pwd import *  def get_username():return getpwuid(getuid())[0]
pwd.py
g:\django\hello>gunicorn hello.wsgi traceback (most recent call last):   file d:\program files\python\python36\lib\runpy.py, line 193, in _run_module_as_main__main__, mod_spec)   file d:\program files\python\python36\lib\runpy.py, line 85, in _run_codeexec(code, run_globals)   file d:\program files\python\python36\scripts\gunicorn.exe\__main__.py, line 5, in <module>   file d:\program files\python\python36\lib\site-packages\gunicorn\app\wsgiapp.py, line 10, in <module>from gunicorn.app.base import application   file d:\program files\python\python36\lib\site-packages\gunicorn\app\base.py, line 13, in <module>from gunicorn.arbiter import arbiter   file d:\program files\python\python36\lib\site-packages\gunicorn\arbiter.py, line 18, in <module>from gunicorn import sock, systemd, util   file d:\program files\python\python36\lib\site-packages\gunicorn\sock.py, line 101, in <module>class unixsocket(basesocket):   file d:\program files\python\python36\lib\site-packages\gunicorn\sock.py, line 103, in unixsocket     family = socket.af_unix attributeerror: module 'socket' has no attribute 'af_unix'
attributeerror: module 'socket' has no attribute 'af_unix'
2.win10平台自带ubuntu遇到的坑
win10开启linux系统:首先,在win10设置-->安全与更新-->针对开发人员-->开发人员模式-->打钩,然后在控制面板-->程序个功能-->启动或关闭windows功能-->适用于linux的windows子系统(bata)-->打勾,最后用管理员运行shell-->输入cmd-->输入bash-->按照操作下载安装linux。
安装完毕后,用管理员身份shell进入cmd,输入bash进入linux系统。系统默认安装的有python2.7和python3.5。python默认启动的是python2,可以设置默认python为python3:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 100  sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 200
按照套路先安装pip3(如果直接安装pip是默认安装在python2中的):sudo apt-get install python3-pip,接着用pip安装django:sudo pip3 install django,最后用pip安装gunicorn:sudo pip3 install gunicorn (现在自动安装的版本是19.7.1)。环境安装完毕,切换到项目地址:cd /mnt/g/django/hello,然后输入gunicorn hello.wsgi ,结果又出错了。oserror: [errno 92] protocol not available ,后来折腾卸载pip发现gunicorn 依赖于python-gunicorn(19.4.5),所以卸载了gunicorn(19.7.1):pip3 uninstall gunicorn ,然后安装了gunicorn(19.4.5):pip3 install gunicorn==19.4.5。然后命令gunicorn hello.wsgi 启动成功,没有出现failed to find application,成功访问了127.0.0.1:8000/admin/ 。
lee@kein:/mnt/g/django/hello$ gunicorn hello.wsgi [2017-07-16 15:16:25 +0800] [428] [info] starting gunicorn 19.7.1traceback (most recent call last):   file /usr/local/lib/python3.5/dist-packages/gunicorn/sock.py, line 44, in set_options     sock.setsockopt(socket.sol_socket, socket.so_reuseport, 1) oserror: [errno 92] protocol not available during handling of the above exception, another exception occurred: traceback (most recent call last):   file /usr/local/bin/gunicorn, line 11, in <module>sys.exit(run())   file /usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py, line 74, in run     wsgiapplication(%(prog)s [options] [app_module]).run()   file /usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py, line 203, in run     super(application, self).run()   file /usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py, line 72, in run     arbiter(self).run()   file /usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py, line 198, in run     self.start()   file /usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py, line 157, in start     self.listeners = sock.create_sockets(self.cfg, self.log, fds)   file /usr/local/lib/python3.5/dist-packages/gunicorn/sock.py, line 180, in create_sockets     sock = sock_type(addr, conf, log)   file /usr/local/lib/python3.5/dist-packages/gunicorn/sock.py, line 32, in __init__self.sock = self.set_options(sock, bound=bound)   file /usr/local/lib/python3.5/dist-packages/gunicorn/sock.py, line 89, in set_optionsreturn super(tcpsocket, self).set_options(sock, bound=bound)   file /usr/local/lib/python3.5/dist-packages/gunicorn/sock.py, line 46, in set_optionsif err[0] not in (errno.enoprotoopt, errno.einval): typeerror: 'oserror' object is not subscriptable
oserror: [errno 92] protocol not available
lee@kein:/mnt/g/django/hello$ gunicorn hello.wsgi [2017-07-16 15:22:16 +0800] [470] [info] starting gunicorn 19.4.5[2017-07-16 15:22:16 +0800] [470] [info] listening at: http://127.0.0.1:8000 (470) [2017-07-16 15:22:16 +0800] [470] [info] using worker: sync [2017-07-16 15:22:17 +0800] [473] [info] booting worker with pid: 473not found: /static/admin/css/base.css not found: /static/admin/css/login.css
3.虚拟机安装ubuntu系统,按照上面的套路,成功一次性启动了服务。也反过来思考win10上遇到的问题,成功把坑2填上了。
另外的一些问题是:
1.django 在linux里面无法安装mysqlclient,这个时候安装pymysql,运行django项目会报错,只需要在一个文件加入一句话即可。
项目根目录下,找到对应app目录下的__init__.py,添加这一句:  
import pymysql   
pymysql.install_as_mysqldb()  
例子:  
我的项目是eagle,在eagle下有manage.py这个文件,同时还有你自己的app,在这个app下有view.py这个文件。  
2.nginx里面的static格式注意,使用location /static/ 和 location /static 加载时是不同的,要根据html中的格式符合。
3.搞了一天才了解的一个问题,大部分教程都是说开启uwsgi --ini eagle.ini链接uwsgi和django,然后开启nginx,但是,
如果你的eagle.ini中没有添加log路径,不会被后台运行,这个时候只能关闭uwsgi,
然后开启nginx,当你在浏览器输入地址后,发现502错误,查看/var/log/nginx/myweb_error.log,发现报错息 ,恭喜你入坑了。
原因是刚刚的uwsgi被关闭,你没有重启,网上的很多人都不知道这个原因。。。
坑爹啊,乱七八糟的回答。根据我的方式,有两种方法,一种是开启nginx后再运行
uwsgi --ini eagle.ini,一种是在eagle.ini添加log路径后会自动后台运行
以上就是django遇到的地雷,大家要注意的详细内容。
其它类似信息

推荐信息