<h2>警告:可能已过时</h2>
<p>下面的<code>settings.py</code>代表了Heroku在2015年最初撰写此答案时的文档内容。虽然我相对确定这里提供的<code>ALLOWED_HOSTS</code>设置是安全的,但在复制这些设置的任何其他设置之前,请查阅<a href="https://devcenter.heroku.com/articles/getting-started-with-python" rel="nofollow noreferrer">the up-to-date docs</a>!</p>
<p>原答案如下。更多信息见下文。</p>
<hr/>
<p>这正是你应该做的,每<a href="https://devcenter.heroku.com/articles/getting-started-with-django#django-settings" rel="nofollow noreferrer">Getting Started with Django on Heroku</a>:</p>
<h2>设置.py</h2>
<pre><code># Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
</code></pre>
<hr/>
<h2>2018年更新</h2>
<p>上面的链接不再有效,因为Heroku现在对入门文档的格式有点不同,它提供了预先构建的示例repo,而不是文档中的代码示例。当前的<a href="https://github.com/heroku/python-getting-started/blob/master/gettingstarted/settings.py#L30" rel="nofollow noreferrer">Python Getting Started Repo</a>有<code>ALLOWED_HOSTS = []</code>,但也有<code>DEBUG = True</code>,根据<a href="https://docs.djangoproject.com/en/2.1/ref/settings/#allowed-hosts" rel="nofollow noreferrer">the Django 2.1 docs</a>触发了一个特殊情况</p>
<pre><code>ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]']
</code></pre>
<p>由于<code>DEBUG = True</code>在生产中不被推荐或是一个好主意,这个答案中的原始建议仍然是Heroku应用程序的生产就绪解决方案。在决定做什么之前,一定要阅读并理解<a href="https://stackoverflow.com/a/49374520/2588818">Charlie Weems' brief answer</a>。</p>
<p>完全公开:我没有在Django的最新版本中构建一个生产版Heroku应用程序。YMMV:)</p>