在PythonAnywhere上运行Django
不同版本。
-
- Python: 3.4.0
- Django: 1.8.2
创建一个适用于python3的虚拟环境。
我要打开终端。
在终端上创建一个虚拟环境。
08:20 ~ $ virtualenv --python="python3" env
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in env/bin/python3
Also creating executable in env/bin/python
Installing setuptools, pip, wheel...done.
08:21 ~ $
激活虚拟环境 virtualenv。
08:21 ~ $ source env/bin/activate
(env)08:21 ~ $
安装Django。
使用pip install命令安装Django – 用pip install django安装Django。
(env)08:21 ~ $ pip install django
Collecting django
Installing collected packages: django
Successfully installed django-1.8.2
创建一个项目
使用命令”python manage.py startproject PROJECTNAME”来创建一个名为PROJECTNAME的项目。
(env)08:27 ~ $ django-admin startproject djexample
(env)08:27 ~ $
执行syncdb
切换到已创建项目的目录。
(env)08:34 ~ $ cd djexample/
暂时进行syncdb操作。
(env)08:35 ~/djexample $ python manage.py syncdb
/home/TakesxiSximada/env/lib/python3.4/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The sync
db command will be removed in Django 1.9
warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: auth, admin, contenttypes, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
No migrations to apply.
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'takesxisximada'): ADMINISTORATOR_NAME
Email address: your.email@mail.address
Password:
Password (again):
Superuser created successfully.
(env)08:35 ~/djexample $
将其在网络上公开
应用程序的设置
选择要使用的Web框架。
虽然有Django,但选择它将导致使用Python2.7和Django1.6,因此选择手动配置。
选择Python的版本。只能选择Python3.4。
现在,我们已经能够在PythonAnywhere上管理新的djexample。
虚拟环境的配置
按“输入自定义的virtualenv路径”按钮,可以设置使用自己创建的virtualenv。
配置wsgi文件
因为被读取的WSGI文件是固定的,所以我们将替换位于/var/www目录下的WSGI文件。
暂时先备份旧文件。
(env)08:55 ~/djexample $ cp /var/www/takesxisximada_pythonanywhere_com_wsgi.py /var/www/takesxisximada_pythonanywhere_com_wsgi.py.old
(env)08:55 ~/djexample $
将djexample的wsgi.py原封不动地复制到/var/www目录下,与原文件同名。
(env)08:55 ~/djexample $ cp djexample/wsgi.py /var/www/takesxisximada_pythonanywhere_com_wsgi.py
(env)08:56 ~/djexample $
然后将其改写如下。
"""
WSGI config for djexample project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""
import os
import sys
from django.core.wsgi import get_wsgi_application
path = '/home/TakesxiSximada/djexample'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'djexample.settings'
application = get_wsgi_application()
实际上,我加入了以下内容。
path = '/home/TakesxiSximada/djexample'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'djexample.settings'
添加静态文件的配置
在settings.py中添加STATIC_ROOT。
(env)09:47 ~/djexample $ tail djexample/settings.py
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
在urls.py中添加静态文件的配置
(env)09:48 ~/djexample $ cat djexample/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
进行collectstatic操作
使用collectstatic命令来收集静态文件。
(env)09:50 ~/djexample $ python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
/home/TakesxiSximada/djexample/static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
~省略~
(env)09:50 ~/djexample $
访问管理界面
重新启动应用程序
看一看
访问http://takesxisximada.pythonanywhere.com/admin。
太好了!