欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python Crash Course读书笔记 - 第20章:STYLING AND DEPLOYING AN APP

程序员文章站 2022-07-14 12:41:24
...

美化项目

不太好翻译,就直接用Stying了。或翻译为美化,格式化
Styling可以使你的应用在不同设备上正常显示。

django-bootstrap4可以下载引导程序(bootstrap)并为项目所用。

(ll_env) $ pip install django-bootstrap4
Collecting django-bootstrap4
  Downloading https://files.pythonhosted.org/packages/0e/48/0a5198d1b2167eaba43612508dce8e89917c70d74c18af0b0de5840f4a68/django_bootstrap4-1.1.1-py3-none-any.whl
Collecting beautifulsoup4
  Downloading https://files.pythonhosted.org/packages/cb/a1/c698cf319e9cfed6b17376281bd0efc6bfc8465698f54170ef60a485ab5d/beautifulsoup4-4.8.2-py3-none-any.whl (106kB)
     |████████████████████████████████| 112kB 153kB/s 
Collecting soupsieve>=1.2
  Downloading https://files.pythonhosted.org/packages/81/94/03c0f04471fc245d08d0a99f7946ac228ca98da4fa75796c507f61e688c2/soupsieve-1.9.5-py2.py3-none-any.whl
Installing collected packages: soupsieve, beautifulsoup4, django-bootstrap4
Successfully installed beautifulsoup4-4.8.2 django-bootstrap4-1.1.1 soupsieve-1.9.5

然后修改settings.py将其包含进来:

INSTALLED_APPS = [
    # My apps
    'learning_logs',
    'users',
    'bootstrap4',
...

Bootstrap 中包含大量的模板用以造型你的应用。
我们需要重写base.html以使用bootstrap模板,此文件较长,这里就展示了。
不过其中涉及到很多HTML tag,解释可参见这里

使用Bootstrap 的组件jumbotron修改主页index.html和登录页面login.html。过程略。然和测试这两个页面:
Python Crash Course读书笔记 - 第20章:STYLING AND DEPLOYING AN APP
接下来格式化页面topics.htmltopic.html(如下图)。Python Crash Course读书笔记 - 第20章:STYLING AND DEPLOYING AN APP

部署项目

本节会涉及Heroku和Git,因为Heroku要依赖后者。不过我觉得Git更有用。
本节我们将项目部署到Heroku上。先注册个账号。
安装Heroku CLI,参见这里

$ export PATH=$PATH:/usr/local/bin
$ curl https://cli-assets.heroku.com/install.sh | sh
$ heroku --version
heroku/7.36.3 linux-x64 node-v12.13.0

先在虚拟环境下安装所需的包,之后Heroku会为项目安装所需包:

(ll_env)$ pip install psycopg2==2.7.*
(ll_env)$ pip install django-heroku
(ll_env)$ pip install gunicorn
(ll_env) $ pip freeze > requirements.txt
# 项目中所需的包写入文件
(ll_env) $ cat requirements.txt
asgiref==3.2.3
beautifulsoup4==4.8.2
dj-database-url==0.5.0
Django==3.0.2
django-bootstrap4==1.1.1
django-heroku==0.3.1
gunicorn==20.0.4
psycopg2==2.7.7
pytz==2019.3
soupsieve==1.9.5
sqlparse==0.3.0
whitenoise==5.0.1

(这一步不要做,因为Heroku有可能不支持,让他自己决定版本就好)将python版本信息写入runtime.txt文件中,与manage.py同一目录。

(ll_env) $ python --version
Python 3.6.8

修改settings.py,尾部追加以下行:

# Heroku settings.
import django_heroku
django_heroku.settings(locals())

manage.py同一目录,创建文件Procfile:

web: gunicorn learning_log.wsgi --log-file -

下面会涉及点Git。
开始设置

(ll_env) $ git --version
git version 1.8.3.1
(ll_env) $ git config --global user.name "..."
(ll_env) $ git config --global user.email "..."

在创建文件.gitignore,与manage.py同一目录。Git会忽略指定的文件:

(ll_env) $ cat .gitignore
ll_env/ 
__pycache__/
*.sqlite3

提交项目:

(ll_env) $ git init
Initialized empty Git repository in /home/xiaoyu/python_works/learning_log/.git/
(ll_env) $ git add .
(ll_env) $ git commit -am "Ready for deployment to heroku."
[master (root-commit) ed995d2] Ready for deployment to heroku.
 4739 files changed, 681803 insertions(+)

(ll_env) $ git status
# On branch master
nothing to commit, working directory clean

...
(ll_env) $ git ls-files |wc -l

(ll_env) $ git rm -r --cached ll_env

(ll_env) $ heroku login
(ll_env) $ heroku create
(ll_env) $ git push heroku master

(ll_env) $ heroku ps
Free dyno hours quota remaining this month: 550h 0m (100%)
Free dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping

=== web (Free): gunicorn learning_log.wsgi --log-file - (1)
web.1: up 2020/01/22 15:58:55 +0800 (~ 40s ago)

(ll_env) $ heroku open

Python Crash Course读书笔记 - 第20章:STYLING AND DEPLOYING AN APP
页面看到了,但数据库还未迁移。迁移数据库不会迁移其中的Topic等数据,包括超级用户:

(ll_env) $ heroku run python manage.py migrate
Running python manage.py migrate on ⬢ morning-ocean-28768... up, run.8493 (Free)

超级用户并没有迁移过去,因此先创建超级用户:

(ll_env) $ heroku run bash
Running bash on ⬢ morning-ocean-28768... up, run.3999 (Free)
~ $ python manage.py createsuperuser
/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Username (leave blank to use 'u18314'): ll_admin
Email address:  
Password: 
Password (again): 
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
~ $ exit
exit

希望更名为用户友好的URL,不过因为重名而失败,因此换了另一名字:

(ll_env) $ heroku apps:rename learning-log
Renaming morning-ocean-28768 to learning-log... !
 ▸    Name learning-log is already taken

(ll_env) $ heroku apps:rename learning-log-demo
Renaming morning-ocean-28768 to learning-log-demo... done
https://learning-log-demo.herokuapp.com/ | https://git.heroku.com/learning-log-demo.git
Git remote heroku updated
 ▸    Don't forget to update git remotes for all other local checkouts of the app.

这样,之前的主页https://morning-ocean-28768.herokuapp.com/不再有效,并变为了https://learning-log-demo.herokuapp.com/

解决一个安全问题。之前将DEBUG设为True.现在改为根据环境变量控制。
修改settings.py

...
if os.environ.get('DEBUG') == 'TRUE':
    DEBUG = True
elif os.environ.get('DEBUG') == 'FALSE':
    DEBUG = False
...

然后提交并设置环境变量以测试:

(ll_env)$ git commit -am "Set DEBUG based on environment variables."
(ll_env)$ git push heroku master
(ll_env)$ heroku config:set DEBUG='FALSE'

以下是定制错误页面。略。
最后再heroku网站删除应用。

相关标签: Python