Heroku 和 Django

Heroku and Django

我正在尝试按照本教程进行操作:http://tutorial.djangogirls.org/en/index.html

我完成了这部分:http://tutorial.djangogirls.org/en/deploy/README.html

我要通过 git 将它推送到 heroku。我熟悉 git 而不是 heroku,虽然我知道 python 我是 django 初学者。

当我执行命令 git push heroku master 时,我得到了阻止应用程序部署的输出。

这是我收到的错误:

(myvenv) $> git push heroku master
Counting objects: 19, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (19/19), 3.81 KiB | 0 bytes/s, done.
Total 19 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing runtime (python-3.4.1)
remote: -----> Installing dependencies with pip
remote:        Exception:
remote:        Traceback (most recent call last):
remote:          File "/app/.heroku/python/lib/python3.4/site-packages/pip-      6.0.6-py3.4.egg/pip/basecommand.py", lin
, in main
remote:            status = self.run(options, args)
remote:          File "/app/.heroku/python/lib/python3.4/site-packages/pip-    6.0.6-py3.4.egg/pip/commands/install.py"
e 321, in run
remote:            finder=finder, options=options, session=session):
remote:          File "/app/.heroku/python/lib/python3.4/site-packages/pip-6.0.6-py3.4.egg/pip/req/req_file.py", li
, in parse_requirements
remote:            session=session,
remote:          File "/app/.heroku/python/lib/python3.4/site-packages/pip-  6.0.6-py3.4.egg/pip/download.py", line 4
n get_file_content
remote:            content = f.read()
remote:          File "/app/.heroku/python/lib/python3.4/codecs.py", line   313, in decode
remote:            (result, consumed) = self._buffer_decode(data, self.errors, final)
remote:        UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in      position 0: invalid start byte
remote:
remote:
remote:  !     Push rejected, failed to compile Python app
remote:
remote: Verifying deploy...
remote:
remote: !       Push rejected to chsdjangoblog.
remote:
To https://git.heroku.com/chsdjangoblog.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/chsdjangoblog.git'

有人知道为什么会这样吗? heroku 似乎很好用,有没有更好的 alternative/what 是 heroku 的最佳用例?我真的只想解决这个问题,这样我就可以继续教程了。学习 django 一直是我的一个目标,因为我厌倦了 Word Press 和 PHP 开发,并且一直是 Python 的爱好者。

在我尝试下一步时出现该错误:heroku ps:scale web=1 我得到以下输出:

Scaling dynos... failed
! App mus tbe deployed before dynos can be scaled.

提前致谢。

编辑:

这是我的 requirements.txt:

Django==1.8
dj-database-url==0.3.0
gunicorn==19.3.0
heroku==0.1.4
python-dateutil==1.5
requests==2.6.0
whitenoise==1.0.6
psycopg2==2.5.4`

我试过保存为 UTF-8、ANSI、UTF-16。给他们所有人的信息都是一样的。我什至在没有复制粘贴的情况下重写了它。为什么无论编码如何,我的第一个字节总是 0xff? heroku 期待什么,是否有 way/tool 来检查 txt 文件中的字节?

看到这个答案:Deploying Django/Python 3.4 to Heroku

pip 正在崩溃,因为您的 requirements.txt 文件编码错误。将其保存为 UTF-8 ANSI 编码。

在这个问题的其他贡献者的肩膀上,您的 requirements.txt 文件似乎编码为 UTF-16 little endian。

0xFF 是 UTF-16-LE 的 Byte Order Mark 的第一个字符,第二个字符是 0xFE。回溯表明第一个字符是位置 0 的 0xFF,并且在 Windows 中常见的是将文件存储为带有 BOM 的 UTF-16。

尝试将 requirements.txt 文件保存为 UTF-8 BOM,或保存为 ASCII。简单的旧 notepad.exe 可能会成功。

编辑

无法在记事本中使用,因此请改用 Python 3:

with open('requirements.txt', encoding='utf-16') as old, open('requirements_new.txt', 'w', encoding='utf-8') as new:
    new.write(old.read())

requirements_new.txt 现在将被编码为 UTF-8 并且应该可以工作(无论如何它最终可能会成为 ASCII)。

请注意,这是基于其他人的评论和回答,这些评论和回答表明麻烦的文件是 requirements.txt

我修复了它,无论我选择什么编码,问题似乎只有在 windows 上生成 requirements.txt 时才会出现。我在 Linux 上以 ascii 格式生成了文件并且它有效。然后我将文件传输到 windows,它在那里也能正常工作。因此,问题一定是评论中提到的 requirements.txt 编码。但是,正确的编码似乎是 ASCII。