简体   繁体   English

Heroku和Django运行服务器

[英]Heroku and Django Running Server

I'm following this tutorial: http://tutorial.djangogirls.org/en/domain/README.html 我正在关注本教程: http : //tutorial.djangogirls.org/en/domain/README.html

When I do python manage.py runserver it works fine. 当我做python manage.py runserver它工作正常。 It also works when I run 我跑步时也可以

heroku ps:scale web=1

then 然后

heroku open

with python manage.py runserver it shows my blog posts and everything that I had added. 使用python manage.py runserver可以显示我的博客文章以及我添加的所有内容。 But when I run the server with heroku open there are no posts as if the database is missing or something. 但是,当我在heroku open运行服务器时,没有发布,好像数据库丢失了一样。

Why is that? 这是为什么? Why do the two commands launch the same web page but with different posts/different databases? 为什么这两个命令启动同一个网页,但帖子/数据库不同?

Which brings me to my follow up question: how do I know when I need to run migrate or makemigrations again for the server? 这使我想到了后续问题:如何知道何时需要再次为服务器运行迁移或进行迁移? Would doing so fix the problem? 这样可以解决问题吗? And what exactly do those commands do/why are they necessary? 这些命令究竟是做什么的/为什么有必要?

Thanks 谢谢

EDIT: 编辑:

Bonus question: Why do my posts display in descending order of time? 奖励问题:为什么我的帖子按时间降序显示? The new posts are on the bottom of the page rather than the top. 新帖子位于页面底部,而不是顶部。 How can I change this? 我该如何更改?

There is a difference between your local development and your deployed project. 您的本地开发和部署的项目之间存在差异。 I assume you created your posts local. 我认为您是在本地创建帖子的。 So they are saved local into your database. 因此它们被本地保存到您的数据库中。 Local you use a filebased database defined in the settings 'django.db.backends.sqlite3' that means when you run manage.py syncdb the file is created with all tables inside. 在本地,您使用设置'django.db.backends.sqlite3'中定义的基于文件的数据库,这意味着当您运行manage.py syncdb ,将在其中创建所有表的文件。 When you deploy your code to heroku the code is pushed to the server and runs from this place. 当您将代码部署到heroku时,代码被推送到服务器并从此位置运行。 This can be everywhere, so it can't connect to your local databasefile. 它可以随处可见,因此无法连接到本地数据库文件。 For your project you has to setup a database on heroku as well. 对于您的项目,您还必须在heroku上建立一个数据库。 I recommend to read this article . 我建议阅读这篇文章 When you want to transfer your data you can create a database dump local and load all data to you heroku database. 当您要传输数据时,可以在本地创建数据库转储并将所有数据加载到您的heroku数据库中。 Described here and here . 在这里这里描述。

Another point, you don't run the server with heroku open or by fire python manage.py runserver . 还有一点,您不会在heroku open或通过fire python manage.py runserver运行服务器。 The heroku server is automatically started when your git push heroku master is done. 当您的git push heroku master完成后,heroku服务器将自动启动。 It use the configs from your Procfile . 它使用Procfile的配置。

When you want to migrate your heroku database you has to run heroku run python manage.py migrate <app_name> than the migration is done remotely on the heroku server. 当您要迁移heroku数据库时,必须运行heroku run python manage.py migrate <app_name> migration heroku run python manage.py migrate <app_name>不是在heroku服务器上远程完成迁移。 You have to run this command everytime you changed a model and added a migrationfile with python manage.py makemigration <app_name> . 每次更改模型并使用python manage.py makemigration <app_name>添加迁移文件时,都必须运行此命令。 When you did this you has to migrate you database local and remote. 执行此操作时,必须将数据库本地和远程迁移。 That means that you change the database structure to match your models. 这意味着您更改数据库结构以匹配您的模型。 Remember the models are only a abstraction(orm) of your database. 请记住,模型只是数据库的抽象(orm)。

I don't know your project but the order sees legit. 我不知道您的项目,但订单合法。 Try to imagine this as rows. 尝试将其想象为行。 first row comes first. 第一排在前。 So the last entered row is on the bottom. 因此,最后输入的行在底部。 You can change the order of the queryset with something like .order_by('-id') . 您可以使用.order_by('-id')类的方法更改查询集的顺序。 So you get all entries in reverse order. 因此,您将获得相反顺序的所有条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM