pythonanywhere是一个面向python开发的云平台
它为初学者提供了一个基本的空间,mysql数据库,一个二级域名,以及相关教程。。
来看看怎样在 pythonanywhere 创建一个 django 应用吧!
1)
点右上角的 Help,进入 https://www.pythonanywhere.com/wiki/
2)
可以看到有一系列教程主题。
找到 I want to follow the Django Tutorial
点击后,页面上方会出现指导页面,跟随指示就可以了。
其中一步是编辑 wsgi.py,其内容如下,其中不仅包含django,也有flask。
现在 uncomment 掉 django 的相应部分,同时记得删掉前面HELLO_WORLD那段。。
# This file contains the WSGI configuration required to serve up your
# web application at http://slowlight.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# +++++++++++ GENERAL DEBUGGING TIPS +++++++++++
# getting imports and sys.path right can be fiddly!
# We've tried to collect some general tips here:
# https://www.pythonanywhere.com/wiki/DebuggingImportError
# +++++++++++ HELLO WORLD +++++++++++
# A little pure-wsgi hello world we've cooked up, just
# to prove everything works. You should delete this
# code to get your own working.
HELLO_WORLD = """<html>
<head>
<title>Python Anywhere hosted web application</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>
This is the default welcome page for a
<a href="https://www.pythonanywhere.com/">PythonAnywhere</a>
hosted web application.
</p>
<p>
Find out more about how to configure your own web application
by visiting the <a href="https://www.pythonanywhere.com/web_app_setup/">web app setup</a> page
</p>
</body>
</html>"""
def application(environ, start_response):
if environ.get('PATH_INFO') == '/':
status = '200 OK'
content = HELLO_WORLD
else:
status = '404 NOT FOUND'
content = 'Page not found.'
response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(content)))]
start_response(status, response_headers)
yield content.encode('utf8')
# Below are templates for Django and Flask. You should update the file
# appropriately for the web framework you're using, and then
# click the 'Reload /yourdomain.com/' button on the 'Web' tab to make your site
# live.
# +++++++++++ VIRTUALENV +++++++++++
# If you want to use a virtualenv, set its path on the web app setup tab.
# Then come back here and import your application object as per the
# instructions below
# +++++++++++ CUSTOM WSGI +++++++++++
# If you have a WSGI file that you want to serve using PythonAnywhere, perhaps
# in your home directory under version control, then use something like this:
#
#import os
#import sys
#
#path = '/home/slowlight/path/to/my/app
#if path not in sys.path:
# sys.path.append(path)
#
#from my_wsgi_file import application
# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
#import os
#import sys
#
## assuming your django settings file is at '/home/slowlight/mysite/mysite/settings.py'
## and your manage.py is is at '/home/slowlight/mysite/manage.py'
#path = '/home/slowlight/mysite'
#if path not in sys.path:
# sys.path.append(path)
#
#os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
#
## then, for django >=1.5:
#from django.core.wsgi import get_wsgi_application
#application = get_wsgi_application()
## or, for older django <=1.4
#import django.core.handlers.wsgi
#application = django.core.handlers.wsgi.WSGIHandler()
# +++++++++++ FLASK +++++++++++
# Flask works like any other wsgi-compatible framework, we just need
# to import the application. Often flask apps are called "app" so we
# may need to rename it during the import:
#
#
#import os
#import sys
#
#path = '/home/slowlight/path/to/flask_app_directory'
#if path not in sys.path:
# sys.path.append(path)
#
#from main_flask_app_file import app as application
#
# NB -- many Flask guides suggest you use a file called run.py; that's
# not necessary on PythonAnywhere. And you should make sure your code
# does *not* invoke the flask development server with app.run(), as it
# will prevent your wsgi file from working.
只需要像下面这样就行了
# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
import django
#
## assuming your django settings file is at '/home/slowlight/mysite/mysite/settings.py'
## and your manage.py is is at '/home/slowlight/mysite/manage.py'
path = '/home/slowlight/mysite'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
## for django >=1.5:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
现在你已经会写 wsgi.py 了,有木有 :)
3)
OK,现在打开 bash(在Consoles选项卡, 方便有木有!)
follow 这篇教程来做接下来的配置,以使我们可以使用django1.7或以上版本
首先需要用 virtualenvwrapper 建立一个虚拟python环境。
为什么需要使用虚拟环境,以及怎么用virtualenvwrapper,可以看LZ的另外一篇博文。
# 建立一个名为 django17 的 virtual ENV
# 可以指定你的python版本,比如我的是 python2.7
mkvirtualenv django17 --python=/usr/bin/python3.4
注意你的bash输入的地方前面多了(django17 ),说明正在使用django17这个虚拟python环境。
注意,使用bash调试django的时候,总是开启虚拟环境
使用 workon django17 来启用
# install django, 可以用如 django==1.7.9 的形式来制定版本
# 默认貌似是最新的 1.8
pip install django
#检查是否安装成功,正常则会看到 django 版本号
python -c "import django; print(django.get_version())"
# PS. 如果用 deactivate 退出虚拟环境,再查看版本号,可以发现默认版本是 1.3 的 Orz..
建立新django项目
django-admin.py startproject mysite
打开 mysite/setting.py,做一些配置,首先得让django工作起来,其他的设置可以慢慢学习~
ALLOWED_HOSTS = ['<your_username>.pythonanywhere.com']
保存,回到~/mysite
python managy.py migrate
然后切到 pythonanywhere 的web选项卡,在virtualenv的路径中填入
/home/你的用户名/.virtualenvs/django17
reload一下,你的网站应该可以显示django的默认页面了~