1 00:00:00,450 --> 00:00:01,070 In order for 2 00:00:01,070 --> 00:00:04,570 my Django project to run correctly on PythonAnywhere, I need two things. 3 00:00:04,570 --> 00:00:08,830 I need a list of all the requirements that the project needs and 4 00:00:08,830 --> 00:00:14,630 I need a deploy specific WSGI or wsgi file, and this is 5 00:00:14,630 --> 00:00:18,678 a file that will tell the PythonAnywhere server how to run the project. 6 00:00:18,678 --> 00:00:21,618 So, I'm gonna start by making sure that the correct packages for 7 00:00:21,618 --> 00:00:23,230 my project are installed locally. 8 00:00:23,230 --> 00:00:27,988 So, I have a new virtual environment here and I'm gonna install the package of 9 00:00:27,988 --> 00:00:32,330 this clean fresh brand new, so that I know I have the newest best stuff. 10 00:00:33,730 --> 00:00:38,153 So, first off, I'm gonna run pip install django==1.9.4, 11 00:00:38,153 --> 00:00:40,297 although really 1.9 is fine. 12 00:00:43,010 --> 00:00:47,299 And that's gonna make sure that I have the same version of Django installed that was 13 00:00:47,299 --> 00:00:50,560 used to create the project, in the course where we created it. 14 00:00:50,560 --> 00:00:55,460 I also wanna make sure that I have the WhiteNoise package installed, 15 00:00:55,460 --> 00:00:57,400 which is used for serving static assets. 16 00:00:57,400 --> 00:01:02,800 So to install that I'll do pip install whitenoise, and I want a specific version, 17 00:01:02,800 --> 00:01:06,442 just because this is the one that's currently around. 18 00:01:06,442 --> 00:01:09,800 And it's always a good idea to pin your deployments, or 19 00:01:09,800 --> 00:01:12,950 to specify a Python package version for each thing. 20 00:01:14,590 --> 00:01:18,550 Now, WhiteNoise is the recommended package for serving static assets in Python. 21 00:01:18,550 --> 00:01:22,770 It can, and will, serve your assets compressed to a smaller size. 22 00:01:22,770 --> 00:01:27,450 And it will set the correct caching headers, both of these things combined 23 00:01:27,450 --> 00:01:31,905 caching so that the files get held onto by the browser and compressions, 24 00:01:31,905 --> 00:01:37,140 although smaller, will make things a lot nicer for your users, so yee. 25 00:01:37,140 --> 00:01:42,551 Now I'm gonna save both of these things into a file called requirements.txt, 26 00:01:42,551 --> 00:01:47,087 by doing pip freeze, and let's just look at what pip freeze does. 27 00:01:47,087 --> 00:01:52,090 So that tells me that Django 1.9 and WhiteNoise are installed. 28 00:01:52,090 --> 00:01:57,138 If I do pip freeze > requirements.txt, then now, 29 00:01:57,138 --> 00:02:02,528 if I do an ls, you can see I have requirements.txt, and 30 00:02:02,528 --> 00:02:10,130 if I cut that, you can see it has the same output that pip freeze gave me up here. 31 00:02:11,160 --> 00:02:13,400 This is just a quick way of getting those requirements into a file. 32 00:02:14,610 --> 00:02:20,268 Okay, now though, I need the deploy wsgi file and 33 00:02:20,268 --> 00:02:27,154 I'm gonna do that by editing the file in a plain text editor. 34 00:02:27,154 --> 00:02:30,871 Now on this machine I'm gonna be using Atom, but 35 00:02:30,871 --> 00:02:34,960 you can do whatever text editor you would like to use. 36 00:02:37,420 --> 00:02:41,860 And before I go edit any files, I'm gonna copy the file. 37 00:02:41,860 --> 00:02:45,262 So if I go into djangoal/djangoal/, 38 00:02:45,262 --> 00:02:50,928 there is a file in here that is wsgi.py, I'm gonna copy that, so 39 00:02:50,928 --> 00:02:56,020 I'm gonna cp wsgi.py and I'm gonna call it deploy.py. 40 00:02:56,020 --> 00:02:58,920 This is my deployment wsgi file. 41 00:02:58,920 --> 00:03:04,099 All right, so this file, djangoal > djangoal > deploy.wsgi, 42 00:03:04,099 --> 00:03:09,150 this is the file that I'm gonna edit for the server. 43 00:03:09,150 --> 00:03:11,800 So in order for the project to run on PythonAnywhere, 44 00:03:11,800 --> 00:03:15,628 I need to adjust the system path to include the full path to the code. 45 00:03:15,628 --> 00:03:21,265 So, here I need to import sys, 46 00:03:21,265 --> 00:03:25,620 S-Y-S, and then I'm going to set a new variable here called path. 47 00:03:25,620 --> 00:03:32,750 And I'm gonna say path = /home, and then I'm gonna put in my user name, 48 00:03:32,750 --> 00:03:38,120 so I'm gonna put in kennethtreehouse, and then I put in deploying_django, 49 00:03:38,120 --> 00:03:41,790 because that's the name of the project, and then djangogoal because 50 00:03:43,170 --> 00:03:47,130 this is the Git checkout and then this is the project name. 51 00:03:48,820 --> 00:03:54,292 And then I'm gonna say if path not in sys.path, 52 00:03:54,292 --> 00:03:57,919 sys.path.append(path). 53 00:03:57,919 --> 00:04:03,270 So sys.path, is all the locations that Python looks for 54 00:04:03,270 --> 00:04:08,460 code, and so I'm checking to make sure that the current one is in there and 55 00:04:08,460 --> 00:04:10,310 if it's not then to make sure and add it. 56 00:04:10,310 --> 00:04:14,575 I also need to add the lines that will make the project use WhiteNoise. 57 00:04:15,630 --> 00:04:19,838 So the first thing I need to do for that is up here and here, 58 00:04:19,838 --> 00:04:22,430 I need to import WhiteNoise. 59 00:04:22,430 --> 00:04:27,789 So from whitenoise.django import DjangoWhiteNoise, 60 00:04:27,789 --> 00:04:31,917 and I'm gonna add a line here at the bottom, 61 00:04:31,917 --> 00:04:37,860 I already have this application = get_wsgi_application. 62 00:04:37,860 --> 00:04:44,298 I'm gonna add a new one here, application = DjangoWhiteNoise(application). 63 00:04:44,298 --> 00:04:49,401 So what we're doing is, I'm re-wrapping the application variable 64 00:04:49,401 --> 00:04:54,610 with this new wsgi application, named DjangoWhiteNoise. 65 00:04:54,610 --> 00:04:58,900 Now, though I have all the files that PythonAnywhere needs to deploy the app. 66 00:04:58,900 --> 00:05:02,780 So, I wanna get this new code into PythonAnywhere. 67 00:05:02,780 --> 00:05:06,090 So, the first thing that I'm gonna do is I'm gonna send this to GitHub, all right? 68 00:05:06,090 --> 00:05:07,711 So I'm gonna clear this. 69 00:05:07,711 --> 00:05:12,350 And if I do a git status, I can see that I have these two files that aren't there. 70 00:05:12,350 --> 00:05:14,910 So, let me get back out here. 71 00:05:16,510 --> 00:05:19,950 Okay, so I'm gonna add the requirements file and 72 00:05:19,950 --> 00:05:22,978 I'm gonna add the, actually I'm just gonna add dot. 73 00:05:24,250 --> 00:05:28,727 So now if I do git status, both those files are in there, 74 00:05:28,727 --> 00:05:32,062 I'm gonna do a commit and I'm gonna say, 75 00:05:32,062 --> 00:05:35,890 Add the files PythonAnywhere needs to deploy. 76 00:05:38,763 --> 00:05:45,189 And then I'm gonna do a git push origin master. 77 00:05:49,779 --> 00:05:52,145 Okay, now once that's pushed, 78 00:05:52,145 --> 00:05:56,799 I can come back over here to PythonAnywhere, to my console here. 79 00:05:56,799 --> 00:05:59,205 And I can do and git pull. 80 00:06:14,645 --> 00:06:20,090 And if I do an ls here, I should see my requirements file, and there it is, great. 81 00:06:20,090 --> 00:06:27,002 So now I can actually do a, I move that up a little bit, 82 00:06:27,002 --> 00:06:31,970 I can install my packages by doing pip install -r requirements.txt. 83 00:06:31,970 --> 00:06:37,510 Make sure you use that -r, that tells pip to read the requirements.txt. 84 00:06:37,510 --> 00:06:41,770 And once these two packages are installed, Django and WhiteNoise, 85 00:06:41,770 --> 00:06:43,910 I'll have everything that I need installed and 86 00:06:43,910 --> 00:06:47,075 ready to go to actually run the app on PythonAnywhere. 87 00:06:47,075 --> 00:06:51,217 In the next video, I'm gonna wire the PythonAnywhere web server up 88 00:06:51,217 --> 00:06:55,299 to the project and I should be able to see the project running live.