1 00:00:00,810 --> 00:00:05,380 Now that I have the Heroku app ready to hold the djangoal project, 2 00:00:05,380 --> 00:00:08,664 I need to get the djangoal project into the Heroku app. 3 00:00:08,664 --> 00:00:13,050 So the first thing that I wanna do is to set up my local git, I've got the project 4 00:00:13,050 --> 00:00:17,963 here in my local computer inside of a git repo. 5 00:00:17,963 --> 00:00:22,530 I wanna get it to where I can deploy that I wanna push that up to Heroku. 6 00:00:22,530 --> 00:00:25,790 So if I come back over here to the Deploy tab and 7 00:00:25,790 --> 00:00:30,260 I read down through here, I can see this thing about deploying using Heroku Git and 8 00:00:30,260 --> 00:00:32,660 there's this link here for the Heroku CLI. 9 00:00:32,660 --> 00:00:38,400 So I wanna open that up and I need to download and install this. 10 00:00:38,400 --> 00:00:43,080 So there's an OS X installer, just 11 00:00:43,080 --> 00:00:46,050 install whichever version is appropriate for your machine. 12 00:00:47,340 --> 00:00:49,177 So I'm gonna go ahead and open that up. 13 00:00:53,077 --> 00:00:54,865 And just work through. 14 00:00:57,788 --> 00:01:00,640 The installer. 15 00:01:00,640 --> 00:01:05,410 So these Heroku command line tools which is also known as the Heroku tool belt. 16 00:01:05,410 --> 00:01:09,902 It's a set of tools that will allow you to work with and 17 00:01:09,902 --> 00:01:12,190 manage your Heroku apps from the command line. 18 00:01:12,190 --> 00:01:14,570 Now I'm only gonna be scratching the surface of what these tools can do. 19 00:01:14,570 --> 00:01:17,280 I highly recommend that you read the documentation for 20 00:01:17,280 --> 00:01:20,260 the tool belt which is in the teacher's notes. 21 00:01:20,260 --> 00:01:23,890 So back over here in the terminal now that I have the tool belt installed. 22 00:01:23,890 --> 00:01:26,280 Let's see if that shows up. 23 00:01:27,680 --> 00:01:31,330 Yeah, okay, so if I type Heroku that will install the Heroku command 24 00:01:31,330 --> 00:01:35,390 line tools which I already have installed but, all right. 25 00:01:35,390 --> 00:01:38,870 But now I can install my or I can import my Heroku Cardinals. 26 00:01:38,870 --> 00:01:43,537 So I'm gonna put in my credentials 27 00:01:48,236 --> 00:01:50,940 And I'm now logged in and so now it sees all of my apps. 28 00:01:50,940 --> 00:01:54,730 So that's cool, I've got, it's seen all of my stuff. 29 00:01:54,730 --> 00:02:01,200 So now that I'm logged in I can type heroku git:remote -a and 30 00:02:01,200 --> 00:02:03,220 then my app name which is djangoal. 31 00:02:05,530 --> 00:02:08,280 So I said to git remote Heroku to point to that. 32 00:02:08,280 --> 00:02:11,850 So that's nice, thanks for doing that for me Heroku. 33 00:02:11,850 --> 00:02:14,225 Most Heroku deploys work through git pushes, 34 00:02:14,225 --> 00:02:17,650 you'll push your code to Heroku, Heroku will run a build process and 35 00:02:17,650 --> 00:02:20,360 your build process is what actually deploys your app. 36 00:02:21,510 --> 00:02:25,120 First though, there are a few files that Heroku needs in your project in 37 00:02:25,120 --> 00:02:26,270 order to build it correctly. 38 00:02:26,270 --> 00:02:28,620 I'm gonna create those files now. 39 00:02:28,620 --> 00:02:32,650 So in the terminal at the top level of the project, 40 00:02:32,650 --> 00:02:37,640 I'm gonna add a new file called requirements.txt. 41 00:02:37,640 --> 00:02:41,910 And a file named Procfile with a capital P that's important and 42 00:02:41,910 --> 00:02:43,910 a file name runtime.txt. 43 00:02:43,910 --> 00:02:49,250 So this will create three files that tell Heroku how to build and 44 00:02:49,250 --> 00:02:51,460 deploy the project. 45 00:02:51,460 --> 00:02:54,373 Hopefully, you already have django installed in the virtual environment, 46 00:02:54,373 --> 00:02:56,132 I actually don't have it installed here yet. 47 00:02:56,132 --> 00:02:59,760 And so because of that I'm gonna go ahead and install it. 48 00:02:59,760 --> 00:03:07,120 So pip install django 1.9 because I used django 1.9 for creating the project. 49 00:03:08,230 --> 00:03:14,170 I'm gonna need another file named Gunicorn or Gunicorn or Gunicorn. 50 00:03:14,170 --> 00:03:16,890 I've heard all of those and others. 51 00:03:16,890 --> 00:03:20,540 So Gunicorn is a piece of software that sits between a web server and 52 00:03:20,540 --> 00:03:21,620 your Django project. 53 00:03:21,620 --> 00:03:25,550 It helps to run your project in a robust and production ready way and 54 00:03:25,550 --> 00:03:29,480 it's the preferred method for serving Django projects with Horoku. 55 00:03:29,480 --> 00:03:31,790 So I'm gonna do pip install gunicorn. 56 00:03:31,790 --> 00:03:36,430 And on this one I make sure that I'm gonna pin it when I install it but 57 00:03:36,430 --> 00:03:39,880 I'll pin it in pip or in the requirements. 58 00:03:39,880 --> 00:03:47,860 So I'm gonna do pip freeze greater than requirements.txt and what that will do. 59 00:03:47,860 --> 00:03:52,842 Let me open all of this up in Adam, If 60 00:03:52,842 --> 00:03:58,130 we look at the requirements.txt this added the requirements into that file. 61 00:03:58,130 --> 00:04:01,780 Pip freeze shows everything they have installed pip freeze with the greater than 62 00:04:01,780 --> 00:04:06,400 symbol will put everything you have installed listed out into a file. 63 00:04:06,400 --> 00:04:09,280 So those two have both been added, so awesome. 64 00:04:09,280 --> 00:04:12,290 Now while we're here in the text editor I'm gonna go ahead and 65 00:04:12,290 --> 00:04:16,190 have a look at the proc file which is currently empty. 66 00:04:16,190 --> 00:04:19,930 The proc file is pretty specific to Heroku, I've not seen it on any other 67 00:04:19,930 --> 00:04:23,750 hosts and it tells Heroku how to configure the dynos and 68 00:04:23,750 --> 00:04:28,140 the processes the procs for the project. 69 00:04:28,140 --> 00:04:30,950 Now well the number of Dynos they get assigned to a project is controlled 70 00:04:30,950 --> 00:04:32,700 through the Heroku dashboard or 71 00:04:32,700 --> 00:04:37,250 Heroku tool belt the, the procfile creates roles for the Dynos to fill. 72 00:04:37,250 --> 00:04:41,950 For example, in our procfile we're gonna add one Dyno, which is called web. 73 00:04:41,950 --> 00:04:47,670 Web is a special one, it lets HTTP requests come into the Dyno. 74 00:04:47,670 --> 00:04:52,410 And we're gonna tell it to run Gunicorn or Gunicorn and 75 00:04:52,410 --> 00:04:59,201 we're gonna say the pythonpath is djangoal asnd we wanna run djangoal.wsgi, 76 00:04:59,201 --> 00:05:02,710 W-S-G-I and log file. 77 00:05:02,710 --> 00:05:06,475 So this tells her Heroku that we will have one role for 78 00:05:06,475 --> 00:05:08,620 Dynos in our app, the role of web. 79 00:05:08,620 --> 00:05:13,310 And if the web Dyno should run Gunicorn with some options to correctly run 80 00:05:13,310 --> 00:05:14,790 our Django project. 81 00:05:14,790 --> 00:05:18,010 You can replace djangoal with the name of your Django project if you're deploying 82 00:05:18,010 --> 00:05:19,620 a different project. 83 00:05:19,620 --> 00:05:22,170 One thing I noticed, I need to put a space in there. 84 00:05:22,170 --> 00:05:24,470 All right, I'm gonna go ahead and save that. 85 00:05:24,470 --> 00:05:28,190 Now the last file that I created was this runtime.txt file 86 00:05:28,190 --> 00:05:30,740 which you can see is still empty. 87 00:05:30,740 --> 00:05:36,610 By default Heroku runs Python apps on Python 2.7 but we are modern Pythonistas, 88 00:05:36,610 --> 00:05:41,260 we want to run the project on Python 3.5 point whatever right or 89 00:05:41,260 --> 00:05:44,628 three point whatever maybe one in 3.5 by the time you're doing this. 90 00:05:44,628 --> 00:05:48,060 So I'm gonna specify that in the run time and 91 00:05:48,060 --> 00:05:53,770 I do that by setting python-3.5.2 as the runtime. 92 00:05:55,220 --> 00:05:58,360 So now that I have all the files that I need to build and 93 00:05:58,360 --> 00:06:01,440 deploy the app, I should go ahead and push it. 94 00:06:01,440 --> 00:06:06,614 So over here in my terminal I'm gonna do git 95 00:06:06,614 --> 00:06:14,250 add dot git commit -m add files necessary to deploy to heroku. 96 00:06:16,310 --> 00:06:19,930 And then I'm gonna do git push heroku mster. 97 00:06:26,184 --> 00:06:28,823 So your Django project, my Django project you can see here, 98 00:06:28,823 --> 00:06:32,770 is being pushed to Heroku and Heroku is going to start the build process. 99 00:06:32,770 --> 00:06:37,170 It discovers that our project is Python based, see Python app detected. 100 00:06:37,170 --> 00:06:42,400 And then it also sees the specified run time, which is right here, Python 3.5.2. 101 00:06:42,400 --> 00:06:46,395 And it traces all the dependencies that we specified. 102 00:06:46,395 --> 00:06:50,915 But, no, we got an error, the push was rejected. 103 00:06:50,915 --> 00:06:51,675 Well what happened? 104 00:06:51,675 --> 00:06:54,275 Well Heroku detected that we had a Django project and 105 00:06:54,275 --> 00:06:57,735 it tried to call collect static on the Django project. 106 00:06:57,735 --> 00:07:00,645 Now since we haven't specified a static route in the settings, 107 00:07:00,645 --> 00:07:02,345 collect static failed. 108 00:07:02,345 --> 00:07:04,235 We can fix this by adding a static route. 109 00:07:04,235 --> 00:07:07,920 So back in your text editor you wanna go and 110 00:07:07,920 --> 00:07:09,800 you wanna go into djangoal settings.py. 111 00:07:09,800 --> 00:07:14,900 And then down at the bottom you want to add a new line, 112 00:07:14,900 --> 00:07:20,687 I'll add right here above static URL that says static route os.path.join 113 00:07:20,687 --> 00:07:25,640 BASE_DIR and then staticfiles. 114 00:07:25,640 --> 00:07:30,450 Now this is a really common setting and static route is where when Heroku or 115 00:07:30,450 --> 00:07:35,880 anyone else calls collect static, the static file the CSS JavaScript 116 00:07:35,880 --> 00:07:39,605 images things like that will get collected into this directory called staticfiles. 117 00:07:41,220 --> 00:07:46,617 So again I'm gonna git add dot get commit 118 00:07:46,617 --> 00:07:53,140 -m set static route and git push heroku master. 119 00:07:55,090 --> 00:07:59,310 Now the Django project should build and deploy, but I'm gonna make sure. 120 00:07:59,310 --> 00:08:05,464 And let's see, it detects the Python app, it installs Python 3.5.2. 121 00:08:07,597 --> 00:08:11,640 It installs Django, it installs Gunicorn. 122 00:08:11,640 --> 00:08:12,530 It runs collect static. 123 00:08:14,410 --> 00:08:16,310 Sets up the process type. 124 00:08:16,310 --> 00:08:18,289 Compresses everything so it's ready to deploy. 125 00:08:22,717 --> 00:08:27,900 And now you can see that there is a link right here that Heroku gives us. 126 00:08:27,900 --> 00:08:34,070 So we should be able to go to our browser, put that link in, and see the app. 127 00:08:34,070 --> 00:08:35,520 Congratulations, if you followed along, 128 00:08:35,520 --> 00:08:38,180 you should have deployed a Django project to Heroku. 129 00:08:38,180 --> 00:08:40,100 But we're not quite done yet. 130 00:08:40,100 --> 00:08:43,170 This workshop is not over just yet. 131 00:08:43,170 --> 00:08:46,180 You'll notice though, that the site might not look right. 132 00:08:46,180 --> 00:08:49,840 I mean, it looks fairly correct, but there could be some things missing, 133 00:08:49,840 --> 00:08:52,660 maybe some of the static files aren't working just right. 134 00:08:52,660 --> 00:08:55,365 And if I go to URL that I know doesn't exist like say 135 00:08:55,365 --> 00:09:01,922 /doesntexist then I get this error page here that I don't want. 136 00:09:01,922 --> 00:09:04,900 It's got the debug information I don't want all of that. 137 00:09:04,900 --> 00:09:09,370 So in the next couple videos we're gonna handle the static files, we're gonna 138 00:09:09,370 --> 00:09:14,330 handle displaying the errors better and I'm gonna connect to a real database. 139 00:09:14,330 --> 00:09:17,340 I wanna fix all of those things before I let you go and 140 00:09:17,340 --> 00:09:19,589 deploy whatever you wanna to Heroku.