设为首页 收藏本站
查看: 513|回复: 0

[经验分享] Python Lover(4)Django Doc

[复制链接]

尚未签到

发表于 2017-5-3 11:11:47 | 显示全部楼层 |阅读模式
  Python Lover(4)Django Doc - CSS and Deployment

1. Easy Way to Serve the Static Files
Have the static resources under here
easypoll/polls/static/polls/style.css
li a {
    color: green;
}

Adding this on the html template, for example, index.html
{% load staticfiles %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

2. Deploying Django

I used to use nginx + fastcgi, fastcgi implementation is FLUP. I would like to try WSGI this time.(Python Web Server Gateway Interface)

2.1 Choose to Try gunicorn
Install pip first
https://pip.pypa.io/en/latest/installing.html#install-or-upgrade-pip

>wget https://bootstrap.pypa.io/get-pip.py
>sudo python get-pip.py
Password: Requirement already up-to-date: pip in /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages Cleaning up...

The problem is that when I install python, I only soft link the python command. Actually I should link this directory to class path.
/Library/Frameworks/Python.framework/Versions/3.4/bin

Soft link in that directory
>sudo ln -s python3 python
>sudo ln -s pip3 pip

>pip -V
pip 1.5.6 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)

Install gunicorn
http://docs.gunicorn.org/en/latest/install.html

>pip install gunicorn
Downloading/unpacking gunicorn  Downloading gunicorn-19.1.1-py2.py3-none-any.whl (104kB): 104kB downloaded Installing collected packages: gunicorn Successfully installed gunicorn Cleaning up...

Install greenlet
>pip install greenlet

2 Options here
http://eventlet.net/
It uses epoll or kqueue or libevent for highly scalable non-blocking I/O
>pip install eventlet

http://gevent.org/
Fast event loop based on libev (epoll on Linux, kqueue on FreeBSD)
>pip install gevent

It runs on my machine
>gunicorn easypoll.wsgi

Settings 
http://docs.gunicorn.org/en/latest/settings.html#settings
http://docs.gunicorn.org/en/latest/configure.html

Example of the configuration file
https://github.com/rdegges/django-skel/blob/master/gunicorn.py.ini
https://github.com/benoitc/gunicorn/blob/master/examples/example_config.py

>sudo gunicorn -c gunicorn.py.ini easypoll.wsgi
my configuration file should be as follow:
>cat gunicorn.py.ini 
"""gunicorn WSGI server configuration.""" import multiprocessing bind = "127.0.0.1:8000" workers = multiprocessing.cpu_count() * 2 + 1


2.2 Install on ubuntu
Actually I should run it on ubuntu system or similar
>sudo apt-get install python-software-properties
>sudo apt-add-repository ppa:gunicorn/ppa
>sudo apt-get update

Not working on ubuntu14.04
>sudo add-apt-repository --remove ppa:gunicorn/ppa
>sudo apt-get install gunicorn

>sudo apt-get install python-pip

>sudo pip install greenlet
>sudo pip install eventlet
>sudo pip install gevent

>sudo gunicorn -c gunicorn.py.ini easypoll.wsgi

Error Message
ImportError: No module named django.core.wsgi

Solution:
>python -V
Python 2.7.6

>sudo rm -fr python
>sudo ln -s python3 python

Download the django package
>wget https://www.djangoproject.com/m/releases/1.7/Django-1.7.tar.gz

>sudo python setup.py install

Error Message
ImportError: No module named 'setuptools'

Solution:
Install setuptools
https://pypi.python.org/pypi/setuptools

>wget https://pypi.python.org/packages/source/s/setuptools/setuptools-5.7.tar.gz
>cd /home/carl/download/install/setuptools-5.7
>sudo python setup.py install

>cd /home/carl/download/install/Django-1.7
>sudo python setup.py install

Error Message
>gunicorn -c gunicorn.py.ini easypoll.wsgi
Traceback (most recent call last):  File "/usr/bin/gunicorn", line 5, in <module>    from pkg_resources import load_entry_point  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible  File "/usr/local/lib/python3.4/dist-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 2872, in <module>  File "/usr/local/lib/python3.4/dist-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 449, in _build_master  File "/usr/local/lib/python3.4/dist-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 745, in require  File "/usr/local/lib/python3.4/dist-packages/setuptools-5.7-py3.4.egg/pkg_resources.py", line 639, in resolve pkg_resources.DistributionNotFound: gunicorn==17.5

Solution:
>sudo easy_install --upgrade pip
>sudo apt-get install python3.4-dev

>sudo pip install greenlet
>sudo pip install eventlet
>sudo pip install gevent

Should install Gunicorn like this.
>sudo pip install git+https://github.com/benoitc/gunicorn.git

>sudo apt-get install python3-pip

It works then.
>gunicorn -c gunicorn.py.ini easypoll.wsgi

But when I visit the admin page, I get some problem with the static pages.

Static Configuration on easypoll/settings.py
# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.7/howto/static-files/ STATIC_URL = '/static/' STATIC_ROOT = "/home/carl/work/easy/easypoll/static/" STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'django.contrib.staticfiles.finders.FileSystemFinder', )

Copy and Collect all the static files to directly
>python manage.py collectstatic

We can point the nginx to that place if we want.

Command to clean the PYC files.
>pyclean .
  Finally, I found it is fine to using python 2.7.6.

And we can change the conf file like this.
carl@ubuntu-slave2:~/work/easy/easypoll$ cat gunicorn.py.ini """gunicorn WSGI server configuration.""" import multiprocessing bind = "0.0.0.0:8000" workers = multiprocessing.cpu_count() * 2 + 1 #worker_class = 'eventlet' #worker_class = 'sync' worker_class = 'gevent'
  
References:
More Topic
https://docs.djangoproject.com/en/1.7/topics/

Look and Feel
https://docs.djangoproject.com/en/1.7/intro/tutorial06/

How to deploy
https://docs.djangoproject.com/en/1.7/howto/deployment/

Spawn-fcgi, wsgi, fastcgi, cgi
http://lihuipeng.blog.iyunv.com/3064864/890573
http://sillycat.iteye.com/blog/582074
http://lutaf.com/141.htm

http://docs.gunicorn.org/en/latest/deploy.html
https://pip.pypa.io/en/latest/installing.html#install-or-upgrade-pip
http://stackoverflow.com/questions/20181121/django-admin-not-working-ugly-serving-with-nginx-and-gunicorn

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-372506-1-1.html 上篇帖子: Python 2.6.2的.pyc文件格式 下篇帖子: 让Python在Android系统上飞一会儿:第一节 在手机上配置Python运行环境
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表