簡易博客開發(8)----django1.9 博客部署到pythonanywhere上

pythonanywhere只容許用戶的一個應用是免費的,但這已經足夠作一個博客了,開心之餘難免又研究起來,用了一個下午的時間,這個醜陋的博客終於上線了 css

首先你得註冊一個用戶 http://pythonanywhere.com html

註冊完後你能夠在account中瞭解到免費的用戶享受到的權利,500M的自主空間,紅框就是你的權利 python

隨後你就能夠增長一個新的應用,你能夠選擇語言開發環境 mysql

選擇python3你才能夠用最新的django,而我用的是python2.7,因此一開始糾結,後來看到能夠本身搭虛擬環境進行任意搭配,豪不猶豫 react

選擇本身搭建,pythonanywhere有很好的提示,只要按照步驟來就沒問題 linux

進到bash下,太熟悉不過了,就是一個linux臺主機,以前咱們就在centos下面搭的,再搭一次也沒問題,但如今是別人的主機,一些規則仍是要聽的 git

進行的操做以下 github


06:50 ~ $ mkvirtualenv django18                                                                                     #給虛擬主機命名
New python executable in /home/terryding/.virtualenvs/django18/bin/python2.7
Also creating executable in /home/terryding/.virtualenvs/django18/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/terryding/.virtualenvs/django18/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/terryding/.virtualenvs/django18/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/terryding/.virtualenvs/django18/bin/preactivate
virtualenvwrapper.user_scripts creating /home/terryding/.virtualenvs/django18/bin/postactivate
virtualenvwrapper.user_scripts creating /home/terryding/.virtualenvs/django18/bin/get_env_details

(django18) 06:57 ~ $ which pip
/home/terryding/.virtualenvs/django18/bin/pip
(django18) 06:59 ~ $ pip install django                                                                            #pip install本身的django,注意若是不加版本是裝最新的,我按照guide來,當時最新是1.8,因而我就裝成了1.9,不過也不要緊
Collecting django                                                                                                  #若是裝1.8應該是pip install django=1.8.8
/home/terryding/.virtualenvs/django18/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/u
til/ssl_.py:315: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) exten
sion to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificat
e, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/sec
urity.html#snimissingwarning.
  SNIMissingWarning
/home/terryding/.virtualenvs/django18/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/u
til/ssl_.py:120: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from
 configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https:/
/urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading Django-1.9.2-py2.py3-none-any.whl (6.6MB)
    100% |████████████████████████████████| 6.6MB 37kB/s
  successful install django-1.9.2
(django18) 06:59 ~ $ django-admin/py --version
1.9.2 (django18) 06:59 ~ $ pip install mysql-python
(django18) 07:13 ~ $ pip install django-contrib-comments   #評論系統
至此虛擬環境搭好了,開始配置

step 1 導入代碼,我用的是git sql

git clone git@github.com:m8k7j@myblog.git
step 2 編寫適當的wsgi.py,它是django和服務器的網絡接口,注意是寫網站上的,而不是咱們本身的


內容以下:紅框爲你的projectname shell

因爲你用的是本身新建的django,因此你還要填寫virtualenv

配置數據庫mysql,先本身起個密碼,信息全在database頁面中

修改settings.py

#/home/terryding/myblog/myblog/settings.py
 81 DATABASES = {
 82     'default': {
 83         'ENGINE': 'django.db.backends.mysql',
 84         'NAME':  'terryding$blog',
 85         'USER': 'terryding',
 86         'PASSWORD': '********',
 87         'HOST': 'terryding.mysql.pythonanywhere-services.com',
 88     }
 89 }

這樣數據庫就設置好了

設置static file

step 1 修改settings.py的static_root

#/home/terryding/myblog/myblog/settings.py
109 STATIC_URL = '/static/'
110 STATIC_ROOT = '/home/terryding/myblog/blog/static'                                          #寫靜態文件目錄的絕對路徑
111 
112 STATICFILES_DIRS = (
113         os.path.join(BASE_DIR, "static"),
114 )
step 2 在網頁中也須要填寫

最後進行調試,再也不像本身的主機那樣看錯誤信息,而是經過這三個文件進行查看,進行調試,我從error_log中分別找到如下錯誤信息,分別進行調試的

2016-02-15 06:41:31,091 :ImportError: No module named 'MySQLdb'                                  #數據庫設置
2016-02-15 08:01:45,974 :NameError: name 'sys' is not defined                                    #wsgi.py 只import os沒有import sys
2016-02-15 08:28:06,510 :Not Found: /static/pure-layout-blog/css/layouts/blog.css                #static file
pythonanywhere中有不少不知道的均可以在help中search

最後預祝你們新年快樂,這個獨立博客就是給我本身也是給大家的新年禮物,之後會慢慢優化

參考http://v2ex.com/t/195207
相關文章
相關標籤/搜索