Host Your Django Project on Heroku (Summary)
Now, you know how to turn your ideas into live web applications that your friends and family will love. Perhaps, someone from an HR department might stumble upon one of your projects and offer you a job. Signing up for a free Heroku account to host your Django code is one of the best ways to enter the world of cloud computing.
In this course, you learned how to:
- Sign up for a free Heroku account
- Use the Heroku CLI (command-line interface)
- Bootstrap a minimal Django project
- Integrate Git with Heroku
- Connect to Heroku’s PostgreSQL database
- Manage configuration, make new releases and rollbacks
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Congratulations! You’ve made it to the end of this course. I hope that you enjoyed the ride, even though it might have felt a bit fast-paced at times, and I do hope that you learned something useful that will enable you to succeed in your ventures. Before closing this, let’s briefly summarize the steps you went through in this course. You started by signing up for a free Heroku account.
00:21 Then you installed and learned how to use the Heroku CLI, which is a convenient command-line tool allowing you to manage your account and all its resources without ever leaving the terminal. Next, you created a placeholder Django project and configured it to be hosted in the cloud. You’ve seen how Heroku integrates with Git, leveraging your existing knowledge, and letting you focus on writing code instead of keeping the infrastructure in good health. Also, you’ve seen how to manage the configuration of your project running on the remote Heroku environment.
00:53 You’ve connected your Django project to a free PostgreSQL database provisioned by Heroku. Finally, you learned how to make and deploy new releases of your project, troubleshoot problems, and do occasional rollbacks when things go south.
01:08 Of course, there’s much more to explore than this. You only scratched the surface of what’s possible with Heroku.
01:15 And in case you were wondering, Heroku isn’t just for toy projects. Companies do use it for commercial purposes. You don’t need to look far. I mean, you’re watching this video right now on a website which is hosted on Heroku.
01:29 All right! Please leave us a comment if you like this course or ask a question if something wasn’t explained clearly enough, and we’ll be happy to help you.
01:39 Thank you for your attention. Take care and see you next time!
Bartosz Zaczyński RP Team on Jan. 7, 2022
@allapopovarus Thanks, and I’m glad that you enjoyed this course 😊
1) How to move data from local sqlite of the project to pg in Heroku?
In general, you should let Django’s ORM handle your data management through migrations to keep everything consistent. Other than that, SQLite uses a different SQL dialect than PostgreSQL, so you won’t be able to dump your schema in one database management system and import it directly into another:
$ sqlite3 db.sqlite3 .dump > dumpfile.sql
However, if you really insist and know what you’re doing, then you can try using Django’s dumpdata
and loaddata
commands, which use JSON for serialization by default. First, run dumpdata
locally, assuming you’re local Django settings are still hooked up to SQLite:
(venv) $ python manage.py dumpdata > data.json
This will save the contents of your local database as a fixture to a local file named data.json
. Next, you’ll need to start a one-off dyno on Heroku to take advantage of the remote database’s settings:
(venv) $ heroku run bash
Now, you need somehow to transfer your local data.json
file into that dyno. There’s no straightforward way as far as I know, but you can use something like GitHub Gist as a proxy and then download your file with the curl
or wget
command:
~ $ wget https://gist.githubusercontent.com/(...)/data.json
~ $ python manage.py loaddata data.json
Afterwards, you can exit the dyno by hitting [Ctrl] + [C]. If everything goes fine, then you should’ve successfully moved your data from SQLite to Heroku’s PostgreSQL database.
2) How to make a deal with static files of models? The idea was to add objects from admin.
Heroku has an ephemeral or temporary file system, so you can’t save any files directly on the running containers. Instead, you should use an external service such as Amazon S3 and only store the remote file references in your relational database.
3) How to create superuser of the project in Heroku?
As long as you’ve created the tables in Heroku’s database by running the first migration either manually or through the release
process in your Procfile, you can run the createsuperuser
command remotely in the following way:
$ heroku run python manage.py createsuperuser
allapopovarus on Jan. 8, 2022
Thanks, for the answers. In fact, I found solution to upload files from admin using FileField. Which uploads files to static folder of app. It works.
Bartosz Zaczyński RP Team on Jan. 9, 2022
@allapopovarus You’re welcome. I wonder if the static files would still be available after a dyno restart or rescaling. Have you tried restarting your web process with heroku restart
?
allapopovarus on Jan. 9, 2022
You are right… unfortunately. So let’s try with Amazon S3.
Become a Member to join the conversation.
allapopovarus on Jan. 6, 2022
Thank you for this course. I was successful without Google’s help at all. Give me the direction, please:
It seems I’m waiting for the part 2 of the course.