Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Using PythonAnywhere’s MySQL11:26 with Kenneth Love
Using PythonAnywhere’s MySQL
So the deployed project on Python Anywhere is almost ready to share with the world. 0:00 The project's running on Python Anywhere, 0:04 the production's secret key is out of the repository, debug is turned off, and 0:06 the static files have all been collected and are being served. 0:10 The last thing is using a more robust database. 0:13 So over here on my local machine, if I'm in the Jangoal directory and 0:17 I do an LS, then, I'm sorry, jangoal and do an ls. 0:21 There we go. 0:26 There is this db.sqlite3, right? 0:27 There's a database. 0:30 And this database is being included whenever I push to GitHub and 0:32 I pull back down on Python Anywhere. 0:35 Generally speaking, you don't want to be moving around database objects. 0:38 So I'm going to make sure that the database gets out of the repository. 0:41 So in my text editor, I'm going to open up the project's .git.ignore file, 0:45 and I'm going to add a new line to this. 0:51 I'm going to add *.sqlite3, sqlite3, and I'm going to save that. 0:56 And if I am in here and I do a git status, 1:03 I can see that the .git ignore has been modified. 1:06 So I can do git add. 1:10 and the sql lite file should no longer be included in the repo. 1:12 So I will need to set up another database. 1:17 I'm going to use Python Anywhere's Mysql as the database. 1:19 To do that, I need some extra packages in the requirements and 1:22 some extra settings in the deploy settings. 1:25 So first of all in requirements.txt, I'm going to add two new lines. 1:28 The first I'm going to add is mysqlclient. 1:33 This is going to be equal to version 1.3.7. 1:35 The next thing I'm going to to add is dj-database-url, and 1:38 this' going to be version 0.4.1. 1:43 These packages will let me get the database settings from the environment and 1:47 connect to the my sequel database. 1:50 I need to override the default database with the Mysql database, and 1:52 I can do that by adding some lines to my deploy settings and then .py. 1:56 So first up here, I'm going to import dj database. 2:01 Import dj database U.R.L. and then at the bottom of the file, 2:05 I'm going to add D.B. underscore from underscore env. 2:10 equals dj database U.R.L. dot config. 2:14 And then I'm going to do databases default.update db from env. 2:19 So that pulls in the database configuration from the environment, and 2:27 it'll override the default database entry with the correct setting. 2:32 So I'm going to push what I have now to Python Anywhere, and 2:35 then I'll create the database in the PythonAnywhere dashboard. 2:38 So do a git add dot, git commit -m add mysql settings. 2:42 And then git push origin master. 2:49 And then in the PythonAnywhere shell, I want to do a git pull. 2:55 Then I want to do Python, or Pip rather. 3:06 Install requirements. 3:09 pip install dash R requirements.txt. 3:13 And so, that will install the MySQL client and 3:20 the DJ database URL packages, that are needed. 3:23 So now back over here in the dashboard. 3:28 I'm gonna go to the databases tab. 3:31 And then I want to create a new database. 3:36 So the first thing I'm gonna do, is I'm gonna create a new password. 3:41 And then I'll click, initialize MySQL. 3:48 I also need to create a new database, to hold the data for the app, right. 3:58 So, in this create a database section. 4:04 I'm going to create a new database with the name Jingle. 4:07 And the actual database name is going to be, like, 4:11 my name $ sign Jingle. 4:15 So, that's the new database there. 4:19 So now I have the information that I need to build the database URL to be 4:22 parsed by DJ database URL. 4:25 So we're going to be using this URL in two places. 4:27 I'm going to build it in the text editor. 4:30 So I'm going to open up a new file. 4:32 And the U.R.L. is going to be my mysql colon slash slash and 4:35 then my user name Kenneth treehouse colon and then the password and 4:42 then @ and then if I look over here at the host. 4:49 I see the host name is right here. 4:55 So that host slash and 4:56 then the database name which is right here. 5:01 So all in all a very long U.R.L. And so I'm gonna copy that. 5:10 And I need to add the details 5:17 into the local environment and the web app environment. 5:21 So I can start by coming over here to the files section. 5:26 And again going into virtual env's. 5:32 Nvenv and bin and then I want to edit post activate again. 5:34 So I'm going to open that in a new tab. 5:41 And then under this line where I export of the secret key. 5:45 I'm going to add in a new line here. 5:48 I'm going to say export database URL is equal to and then the big long path. 5:49 That I put in. 5:57 And I'm gonna hit Save. 5:58 And I'm going to go back over to here and I'm gonna cd into djangoal. 6:03 And then I'm going to run python manage.py migrate 6:09 --settings=djangol.deploy_settings to test and 6:13 make sure that the database URL worked. 6:18 Let me make sure that this has all the files. 6:34 I forgot to reactivate, workon venv 6:43 Okay, so if you were following along you may have got an error about not 6:54 being able to access the database. 6:59 The problem is this dollar sign, so 7:01 I'm going to go ahead and escape that dollar sign. 7:04 And then I'm going tocopy this whole thing. 7:07 Then I'm come back over here to my post activate and 7:10 I'm gonna make sure that this, In 7:13 here, Has the dollar sign in it. 7:18 I will save. 7:24 And then in the bash console in the directory here that has managed up high, 7:28 I want to work on VE and V. 7:33 I want to reload my virtual environment so that I get that environment variable. 7:35 And I want to do Python managed dot pie migrate and then dash, 7:41 dash, settings, equals jango .deploy settings. 7:46 And I'll see all the migrations being run here. 7:53 So that's great. 7:56 That means connect to the database and all of that stuff. 7:56 While I'm here I want to go ahead and make sure that I've got a super user. 8:00 So Python manage.py createssuperuser And again settings 8:03 equals djangoal.deploy_settings. 8:11 And I wanna give it user name. 8:15 And user name and a password and this should be as it was created. 8:23 Successfully. 8:29 Awesome. 8:31 OK so now the MYSQL database is correctly wired into the pythonanywhere console. 8:31 So the stuff is being able to run. 8:37 I need to make it connect to the project. 8:39 So I'm going to come back over to here back to my Web tab and 8:41 back down here as you can probably guess to the WSGI configuration file 8:46 I'll open that in a new tab and here where I have this, 8:52 these OS environ things I want to add a new one. 8:56 So I want to do os.environ 9:00 database URL is equal to that thing. 9:04 I'm gonna save save. 9:11 And, back on the Web tab, I will reload my app. 9:14 And then, I can come over here and refresh the app, and it should be working. 9:25 Something went wrong. 9:31 I'm gonna bet. Let's look at the error logs. 9:32 What I bet what went wrong though is that database URL. 9:38 No we need to collect static again. 9:43 Let me check these other two log files just in case. 9:50 Nothing in the access one. 9:54 Nothing in the server one. 9:58 Okay, let me try collecting static again. 10:00 Yes that's fine. 10:13 There's the site. 10:20 And if I go to /admin, 10:22 I should be able to log in with the user that I created. 10:29 I may need to change this URL so it doesn't have the slash in it. 10:35 Let's try that. 10:38 Save. 10:42 Reload. 10:45 There we go. 10:49 So for the whisky file you will not need the slash, for 10:56 the post activate you will need the slash. 10:59 And it's because they are interpreted differently, 11:01 the whisky file's interpreted by Python. 11:04 The post activate as interpreted by Bash, and they both deal with dollar 11:06 signs in a different way But that wraps it up for this workshop. 11:10 If you want to have your site just running, then you'll wanna go ahead and 11:15 do this run for three months from today. 11:18 That way you can share your link with the world and let everybody enjoy your site. 11:20 Thanks for watching. 11:25
You need to sign up for Treehouse in order to download course files.Sign up