简体   繁体   中英

Heroku and Django Running Server

I'm following this tutorial: http://tutorial.djangogirls.org/en/domain/README.html

When I do python manage.py runserver it works fine. 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. But when I run the server with heroku open there are no posts as if the database is missing or something.

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. When you deploy your code to heroku the code is pushed to the server and runs from this place. 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. 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. Described here and here .

Another point, you don't run the server with heroku open or by fire python manage.py runserver . The heroku server is automatically started when your git push heroku master is done. It use the configs from your 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. You have to run this command everytime you changed a model and added a migrationfile with 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.

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') . So you get all entries in reverse order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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