Now that we have our PythonAnywhere environment set up to receive our application, we need to make our Django app ready for deployment. In this video, we’ll cover capturing our package dependencies and getting our app ready to serve static assets.
In order for 0:00 my Django project to run correctly on PythonAnywhere, I need two things. 0:01 I need a list of all the requirements that the project needs and 0:04 I need a deploy specific WSGI or wsgi file, and this is 0:08 a file that will tell the PythonAnywhere server how to run the project. 0:14 So, I'm gonna start by making sure that the correct packages for 0:18 my project are installed locally. 0:21 So, I have a new virtual environment here and I'm gonna install the package of 0:23 this clean fresh brand new, so that I know I have the newest best stuff. 0:27 So, first off, I'm gonna run pip install django==1.9.4, 0:33 although really 1.9 is fine. 0:38 And that's gonna make sure that I have the same version of Django installed that was 0:43 used to create the project, in the course where we created it. 0:47 I also wanna make sure that I have the WhiteNoise package installed, 0:50 which is used for serving static assets. 0:55 So to install that I'll do pip install whitenoise, and I want a specific version, 0:57 just because this is the one that's currently around. 1:02 And it's always a good idea to pin your deployments, or 1:06 to specify a Python package version for each thing. 1:09 Now, WhiteNoise is the recommended package for serving static assets in Python. 1:14 It can, and will, serve your assets compressed to a smaller size. 1:18 And it will set the correct caching headers, both of these things combined 1:22 caching so that the files get held onto by the browser and compressions, 1:27 although smaller, will make things a lot nicer for your users, so yee. 1:31 Now I'm gonna save both of these things into a file called requirements.txt, 1:37 by doing pip freeze, and let's just look at what pip freeze does. 1:42 So that tells me that Django 1.9 and WhiteNoise are installed. 1:47 If I do pip freeze > requirements.txt, then now, 1:52 if I do an ls, you can see I have requirements.txt, and 1:57 if I cut that, you can see it has the same output that pip freeze gave me up here. 2:02 This is just a quick way of getting those requirements into a file. 2:11 Okay, now though, I need the deploy wsgi file and 2:14 I'm gonna do that by editing the file in a plain text editor. 2:20 Now on this machine I'm gonna be using Atom, but 2:27 you can do whatever text editor you would like to use. 2:30 And before I go edit any files, I'm gonna copy the file. 2:37 So if I go into djangoal/djangoal/, 2:41 there is a file in here that is wsgi.py, I'm gonna copy that, so 2:45 I'm gonna cp wsgi.py and I'm gonna call it deploy.py. 2:50 This is my deployment wsgi file. 2:56 All right, so this file, djangoal > djangoal > deploy.wsgi, 2:58 this is the file that I'm gonna edit for the server. 3:04 So in order for the project to run on PythonAnywhere, 3:09 I need to adjust the system path to include the full path to the code. 3:11 So, here I need to import sys, 3:15 S-Y-S, and then I'm going to set a new variable here called path. 3:21 And I'm gonna say path = /home, and then I'm gonna put in my user name, 3:25 so I'm gonna put in kennethtreehouse, and then I put in deploying_django, 3:32 because that's the name of the project, and then djangogoal because 3:38 this is the Git checkout and then this is the project name. 3:43 And then I'm gonna say if path not in sys.path, 3:48 sys.path.append(path). 3:54 So sys.path, is all the locations that Python looks for 3:57 code, and so I'm checking to make sure that the current one is in there and 4:03 if it's not then to make sure and add it. 4:08 I also need to add the lines that will make the project use WhiteNoise. 4:10 So the first thing I need to do for that is up here and here, 4:15 I need to import WhiteNoise. 4:19 So from whitenoise.django import DjangoWhiteNoise, 4:22 and I'm gonna add a line here at the bottom, 4:27 I already have this application = get_wsgi_application. 4:31 I'm gonna add a new one here, application = DjangoWhiteNoise(application). 4:37 So what we're doing is, I'm re-wrapping the application variable 4:44 with this new wsgi application, named DjangoWhiteNoise. 4:49 Now, though I have all the files that PythonAnywhere needs to deploy the app. 4:54 So, I wanna get this new code into PythonAnywhere. 4:58 So, the first thing that I'm gonna do is I'm gonna send this to GitHub, all right? 5:02 So I'm gonna clear this. 5:06 And if I do a git status, I can see that I have these two files that aren't there. 5:07 So, let me get back out here. 5:12 Okay, so I'm gonna add the requirements file and 5:16 I'm gonna add the, actually I'm just gonna add dot. 5:19 So now if I do git status, both those files are in there, 5:24 I'm gonna do a commit and I'm gonna say, 5:28 Add the files PythonAnywhere needs to deploy. 5:32 And then I'm gonna do a git push origin master. 5:38 Okay, now once that's pushed, 5:49 I can come back over here to PythonAnywhere, to my console here. 5:52 And I can do and git pull. 5:56 And if I do an ls here, I should see my requirements file, and there it is, great. 6:14 So now I can actually do a, I move that up a little bit, 6:20 I can install my packages by doing pip install -r requirements.txt. 6:27 Make sure you use that -r, that tells pip to read the requirements.txt. 6:31 And once these two packages are installed, Django and WhiteNoise, 6:37 I'll have everything that I need installed and 6:41 ready to go to actually run the app on PythonAnywhere. 6:43 In the next video, I'm gonna wire the PythonAnywhere web server up 6:47 to the project and I should be able to see the project running live. 6:51
You need to sign up for Treehouse in order to download course files.Sign up