gpt4 book ai didi

django - 为什么 django sqlite3 数据库在一台机器上与另一台机器上的工作方式不同?

转载 作者:行者123 更新时间:2023-12-04 00:15:08 25 4
gpt4 key购买 nike

我正在努力学习 Django,但我遇到了一个问题,即我的数据库无法正确传输(或者它正在正确传输而我遗漏了一些东西,这就是我认为正在发生的事情)。

我在 models.py

中有一个 Django 模型 Link
class Link(models.Model):
title = models.CharField(max_length=100)
alt = models.CharField(max_length=100)
dest = models.CharField(max_length=100)
image = models.FilePathField(path='static/misc/link_in_bio/img/')
def __str__(self):
title = self.title + ' Link'
return self.title
class Meta:
ordering = ('title', )

然后我创建了这个模型的实例并确认它们已正确输入数据库。然后在页面的 html 中引用它们,如下所示:

{% for link in links %}
<div id="space">
<div class="w3-container">
<div class="w3-panel" style="padding: 0.06em">
<button class="block" type="button" title="{{link.title}}" onclick="window.open('{{link.dest}}')">
<img src="../{{link.image}}" alt="{{link.alt}}" style="max-height: 7em">
</button>
</div>
</div>
</div>
{% endfor %}

在我的开发机器上,一切都按预期工作,并显示如下: correct screen

然后,当我在 Heroku 上部署它时,一切都正确构建,python manage.py makemigrationspython manage.py migrate 运行正常,但是当我查看页面时它只是显示:incorrect screen

我也无法使用我已经在第一台机器上创建的 super 用户登录到管理页面,我知道这是相关的,但我不知道如何处理它。是否有一些步骤来加载和初始化数据库或其他什么?

完整代码在 github here如果有帮助的话。

感谢您的帮助。

最佳答案

发生这种情况是因为他们的架构文件系统不适合 SQLite3,并且经常导致根本没有数据,从而使您的数据库为空。

Heroku 开发中心有一个 entire page就此主题而言。引用相关部分:

Heroku’s Cedar stack has an ephemeral filesystem. You can write to it,and you can read from it, but the contents will be clearedperiodically. If you were to use SQLite on Heroku, you would lose yourentire database at least once every 24 hours.

Even if Heroku’s disks were persistent running SQLite would still notbe a good fit. Since SQLite does not run as a service, each dyno wouldrun a separate running copy. Each of these copies need their own diskbacked store. This would mean that each dyno powering your app wouldhave a different set of data since the disks are not synchronized.

Heroku 建议在生产环境中使用 PostgreSQL,您会发现很多资源都在讨论如何在 Heroku 上使用它而不是 SQLite。

如果需要,您还可以使用支持 SQLite3 的平台 PythonAnywhere。

关于django - 为什么 django sqlite3 数据库在一台机器上与另一台机器上的工作方式不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64455010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com