Hello, and welcome to a demonstration for Django for Everybody. This is to install Django on PythonAnywhere. In a sense, we're going to just be getting started. This is a bit tricky because there's a tutorial on how to write your first Django application. There's a tutorial on how to set up Django.org on PythonAnywhere. And neither of these are exactly perfect. So what I've got is an assignment that sort of walks you through a series of things that lay down some necessary things for this class. And so the first thing we're going to do is you should have a PythonAnywhere account. And you should be able to log in on the PythonAnywhere account and you should be able to get somewhere. So this is my Dr. Chuck, this is my account that I use to, I use to be a student on this. There's other accounts that I have for the production stuff, but this is one of my student accounts. And so the first thing you're going to do is you're going to use a console and you're going to start a Bash shell. So this is actually Linux. And I love PythonAnywhere, I love the fact that we're learning Linux. Hopefully you'll just get a little more comfort with Linux as we go. But this is where you're going to do a lot of the command line of the system. So we're going to do mkdir virtual environment. You might be able to make this Python 3.7 or Python 3.8. The later that Python is, the better. This is going to take a while because it's going to create what's called a virtual environment that allows you to have your own version of Python, your own set of installed Python libraries. And virtual environments are a wonderful thing in Python. Because then you don't have to ask the owner of this system, which is PythonAnywhere, hey, could you upgrade the Python? Turns out you can upgrade the Python all by yourself. So we will sit and let this, oh, there we go, it ran right away and [SOUND], it ran right away. So let's go do the next thing. The next thing I know takes a long time. So the next step is to install Django. Now, this is pip, it is installing software. So it's going to download a bunch of software, and then it's going to install that software. And it's installing it in our django3 environment, right? And so this is not installing it for the whole system, it's just installing it for us. And so you'll get used to the idea that you've got to work on the Django, in the appropriate Django environment. We're using Django 3.0 in this situation. As of the recording of this, Django 3.0.2 is out. Django 3 is quite nice. So we'll just sit and watch this for a while. So it's finished, and now we have it installed. Now, there's a command that gets us into, This is redundant right now because we're already in it. But work on Django 3 and make sure that you're in it. Then the next thing to do is grab this command and make sure your Django version is right. If that says 3.0.2 or later, then life is good and you have your Django working. Now, I want to also have you install some of the sample code in the class. I'll get this started, and I'll explain what's going on. So this is GitHub, and this is code that I maintain, and I can update this code and I can send it to you. And so this is a lot of sample code. So if you go to dj4e, tab I hit the Tab key to do completion there, there is a bunch of files in here. And this is the kind of sample code that we're going to go through in the class. And so we need to add the requirements. pip, again, is the dependency manager. There's a file requirements.txt that says hey, we need a whole bunch of libraries. And so let's install those libraries. Now, in your first Python applications, you're not going to need all of these libraries. But I want to get them installed into your virtual environment early on so that as we go along, you're not going to add a feature like social authentication, which is this little application right there, like social authentication, and then, oops, we had to install a thing. So I want to get all that stuff installed at the beginning. And this will take a little while, so we'll just pause for a bit. Okay, so that finished. And now what we want to do is run a command, manage.py check. You're going to run this command a lot. And we're in actually a Django application. And this Python manage.py check reads through your files and these files that point to other files, looks for syntax errors. And it's a good way to check the sanity of your application. Because if it won't pass this manage.py check, it's not going to start. We haven't even tried to start this application yet and put it up on the web. For now, that's what we're doing. So don't worry about this message, we'll get to that later. We can say python3 manage makemigrations. And in this one, it's going to say no change detected. And then we're going to do some, we're going to run migrate, and that will create a bunch of database tables. Don't worry too much about that. So we've finished there. Now we're going to actually pick up in the Django application. We're going to basically make a new Django, we're going to make a folder. So we're going to go back to our home directory, cd ~ says go back to home directory. And we're going to make a directory called django_projects. This is so you can have more than one of them. We're going to go into that directory, cd django_projects. And then we're going to start a project my test site. Now what this does is this actually created a whole bunch of files in my test site, okay? Now, we do have to edit one file. I'm going to edit this file. Now, I have a whole thing on how to edit files on PythonAnywhere. I'll do it the hard way, I'll do it the way you probably will do it, and that is use this Files screen. Oops, I have to go back and get my shell, dang it, Console > Open Link in New Tab. You'll find that you'll want to have, go back to that console. So here I am, I want to edit some files, so I can go into django_projects > mysite, mysite. And then settings.py is the file that it's telling me to edit. So it says mysite/mysite, it's a little weird that the name is doubled. You've got to go down to around line 28 and say add quote, star, quote. And that has to do with protecting what systems can talk to this web server, and we're just going to say anybody can do it. Do not set this to false, forget this warning, it's a lie. We are not in production, we are in development. So then we have to say Save, so that will save our file, okay? So let's go back, we had to do that. Here's different ways of editing the file. You can use nano on the command line, and that's cool. I just used this number one way to do that. vi is really cool, but we won't do that. Okay, so now we need to convince PythonAnywhere to run the application, okay? And so that is under another tab, that is under, I'm going to open this in a new tab. So now I have these files, actually I don't need this tab anymore, I should, oops, I should say python3 manage.py check here just to kind of be creative. Yeah, that's good. So you always want to do python3. I'm in the site, the actual website that has manage.py in it, python. Actually, you don't have to say python3 any more, it's after the year 2020. So you can say python, python manage.py check. You'll see me type python3 all the time because old habits die hard during the transition from Python 2 to Python 3. So that's good, that suggests that this application in django_projects is in good shape. So now I have to go look at this web app setup. And what's going on here is this is PythonAnywhere's way of taking the code in that folder and running it and giving you a web address for that. And so the first thing you've got to do is you've got to set this up. And this is correctly set up. You've got to point to the folder that has that manage.py in it. And if I go to the console and type pwd, print working directory, you see home, Django, Dr. Chuck's mysite. And this is the file that has, the folder that has manage.py in it. So these two things have got to be right. This code here has also got to be right. When you see this, it's going to start out really big. Throw it all away, throw the sample away. I give you the entire code to put in there. And again, it's all setting up which site. And then, we also have to set the virtual environment in this Web tab, got to go back to the Web tab. We've got to set this virtual environment. And I want that to be, you've got to go and change the name of it. Don't do the same as me, this is for my virtual environment. So it checks, yep, there's a good virtual environment. So you have this source code, working directory, WSGI configuration file, and virtual environment set properly. We'll come back to this. These are error logs. And if all goes well at this point, let's double-check the assignment. Hit the Reload, it's what it says. So every time you change your code, you have to tell PythonAnywhere, so this takes a little bit. And if all goes well, I will open this URL in a new tab. And it worked, debug is true. Please leave debug true, or else this course is going to be really tiresome for you, okay? So that's the first step, but then we have a little bit more work to do. We're going to add a polls application. So we're going to go back into that, I'm already in that folder, I'm in that folder. And then I'm going to start an application, oops, come back, I'm going to build an application. This is a way to create a bunch of source code. So one of the things about Django is that it comes with batteries included. And so in this particular case, it's writing a bunch of code in a folder named polls, okay? And so that's now in polls. And then we go continue on our, now we're going to pick up in this Django tutorial. So we said startapp polls, we did that. And we're going to edit a file polls/views.py. And we're going to copy this code here, Open a new tab, and django_projects > mysite > polls > views.py. So I'm going to put that code from the tutorial in there, okay? So that code is in a tutorial, going back to the tutorial. It says create a file called urls.py in that same folder and put in this code, so I'll copy this. So I'm going to create a new file here, urls.py, New file. Let's put that stuff in there and save that file. Then we go back and then we are going to go into mysite. So polls/urls.py is an application, mysite is for the entire site. And so I'll grab this code also. This file already exists, so we go mysite > mysite > settings. This is the only folder that has settings.py in it. I wasn't supposed to edit settings, I was supposed to edit urls. And this looks pretty good, we'll just add the line. Well, actually we've got to do all of it because we have to import the include. So this little import include, this little part here was there. So I copied this. These are all, of course, comments. So all these files are just some Python. This is setting a variable called urlpatterns to an array, and path is a thing that returns an object. And so you can go read all the, and you should read all the documentation. And then I will save this, and then let's go back. Now, every time you see here when it says python3 manage runserver, you want to go do python3 manage check. So that's like do everything but run the server, except run the server. And again, you could get errors here, and read those errors. They're tracebacks. They'll say, oh, no comma found. Let me just make a mistake so you see what happens. I'm not making mistakes. Come back, and let's go into this file. Let's forget a parenthesis, okay? Let's save it. Shocking. See? So now when I did a python3 manage.py check, it blows up. And it tells me something's kind of funky around line 22 of urls.py, looky-look. I mean, my editor even told me that, so there we go. So I fix that back up and I say save. And now I go back and I type, by the way, you can hit up arrow and you can run a command again. python3 manage.py finds no issues. So then what do we do to test our application? We go to the web app. Now, we've got these guys set up, we don't have to change those, they're all set up again. So we hit the Reload button, take a little bit of moment. Let me go over to our actual application. Now, this looks like a failure, but it's not, because it turns out that that little bit of code you run is sitting at polls. And so that's a code that you ran. So at this point, you have completed the assignment. I got a little bit of odd errors down there. So we walked through that. It wasn't too bad. Getting things set up is often the hardest bit, but hopefully in a little bit of time you'll have it all working. And then we'll start adding things to our application. So hope this was helpful. Cheers!