Deploy!

Note The following chapter can be sometimes a bit hard to get through. Persist and finish it; deployment is an important part of the website development process. This chapter is placed in the middle of the tutorial so that your mentor can help with the slightly trickier process of getting your website online. This means you can still finish the tutorial on your own if you run out of time.

Until now, your website was only available on your computer. Now you will learn how to deploy it! Deploying is the process of publishing your application on the Internet so people can finally go and see your app. :)

As you learned, a website has to be located on a server. There are a lot of server providers available on the internet, we’re going to use PythonAnywhere. PythonAnywhere is free for small applications that don’t have too many visitors so it’ll definitely be enough for you now.

The other external service we’ll be using is GitHub, which is a code hosting service. There are others out there, but almost all programmers have a GitHub account these days, and now so will you!

These three places will be important to you. Your local computer will be the place where you do development and testing. When you’re happy with the changes, you will place a copy of your program on GitHub. Your website will be on PythonAnywhere and you will update it by getting a new copy of your code from GitHub.

Git

Note If you already did the Installation steps, there’s no need to do this again – you can skip to the next section and start creating your Git repository.

{% include “/deploy/install_git.md” %}

Starting our Git repository

Git tracks changes to a particular set of files in what’s called a code repository (or “repo” for short). Let’s start one for our project. Open up your console and run these commands, in the djangogirls directory:

Note Check your current working directory with a pwd (Mac OS X/Linux) or cd (Windows) command before initializing the repository. You should be in the djangogirls folder.

{% filename %}command-line{% endfilename %}

  1. $ git init
  2. Initialized empty Git repository in ~/djangogirls/.git/
  3. $ git config --global user.name "Your Name"
  4. $ git config --global user.email you@example.com

Initializing the git repository is something we need to do only once per project (and you won’t have to re-enter the username and email ever again).

Git will track changes to all the files and folders in this directory, but there are some files we want it to ignore. We do this by creating a file called .gitignore in the base directory. Open up your editor and create a new file with the following contents:

{% filename %}.gitignore{% endfilename %}

  1. *.pyc
  2. *~
  3. __pycache__
  4. myvenv
  5. db.sqlite3
  6. /static
  7. .DS_Store

And save it as .gitignore in the “djangogirls” folder.

Note The dot at the beginning of the file name is important! If you’re having any difficulty creating it (Macs don’t like you to create files that begin with a dot via the Finder, for example), then use the “Save As” feature in your editor; it’s bulletproof.

Note One of the files you specified in your .gitignore file is db.sqlite3. That file is your local database, where all of your posts are stored. We don’t want to add this to your repository because your website on PythonAnywhere is going to be using a different database. That database could be SQLite, like your development machine, but usually you will use one called MySQL which can deal with a lot more site visitors than SQLite. Either way, by ignoring your SQLite database for the GitHub copy, it means that all of the posts you created so far are going to stay and only be available locally, but you’re going to have to add them again on production. You should think of your local database as a good playground where you can test different things and not be afraid that you’re going to delete your real posts from your blog.

It’s a good idea to use a git status command before git add or whenever you find yourself unsure of what has changed. This will help prevent any surprises from happening, such as wrong files being added or committed. The git status command returns information about any untracked/modified/staged files, the branch status, and much more. The output should be similar to the following:

{% filename %}command-line{% endfilename %}

  1. $ git status
  2. On branch master
  3. Initial commit
  4. Untracked files:
  5. (use "git add <file>..." to include in what will be committed)
  6. .gitignore
  7. blog/
  8. manage.py
  9. mysite/
  10. nothing added to commit but untracked files present (use "git add" to track)

And finally we save our changes. Go to your console and run these commands:

{% filename %}command-line{% endfilename %}

  1. $ git add --all .
  2. $ git commit -m "My Django Girls app, first commit"
  3. [...]
  4. 13 files changed, 200 insertions(+)
  5. create mode 100644 .gitignore
  6. [...]
  7. create mode 100644 mysite/wsgi.py

Pushing your code to GitHub

Go to GitHub.com and sign up for a new, free user account. (If you already did that in the workshop prep, that is great!)

Then, create a new repository, giving it the name “my-first-blog”. Leave the “initialize with a README” checkbox unchecked, leave the .gitignore option blank (we’ve done that manually) and leave the License as None.

Deploy! - 图1

Note The name my-first-blog is important – you could choose something else, but it’s going to occur lots of times in the instructions below, and you’d have to substitute it each time. It’s probably easier to just stick with the name my-first-blog.

On the next screen, you’ll be shown your repo’s clone URL. Choose the “HTTPS” version, copy it, and we’ll paste it into the terminal shortly:

Deploy! - 图2

Now we need to hook up the Git repository on your computer to the one up on GitHub.

Type the following into your console (Replace <your-github-username> with the username you entered when you created your GitHub account, but without the angle-brackets):

{% filename %}command-line{% endfilename %}

  1. $ git remote add origin https://github.com/<your-github-username>/my-first-blog.git
  2. $ git push -u origin master

Enter your GitHub username and password and you should see something like this:

{% filename %}command-line{% endfilename %}

  1. Username for 'https://github.com': ola
  2. Password for 'https://ola@github.com':
  3. Counting objects: 6, done.
  4. Writing objects: 100% (6/6), 200 bytes | 0 bytes/s, done.
  5. Total 3 (delta 0), reused 0 (delta 0)
  6. To https://github.com/ola/my-first-blog.git
  7. * [new branch] master -> master
  8. Branch master set up to track remote branch master from origin.

Your code is now on GitHub. Go and check it out! You’ll find it’s in fine company – Django, the Django Girls Tutorial, and many other great open source software projects also host their code on GitHub. :)

Setting up our blog on PythonAnywhere

Sign up for a PythonAnywhere account

Note You might have already created a PythonAnywhere account earlier during the install steps – if so, no need to do it again.

{% include “/deploy/signup_pythonanywhere.md” %}

Configuring our site on PythonAnywhere

Go back to the main PythonAnywhere Dashboard by clicking on the logo, and choose the option to start a “Bash” console – that’s the PythonAnywhere version of a command line, just like the one on your computer.

Pointing at Bash in the New Console section

Note PythonAnywhere is based on Linux, so if you’re on Windows, the console will look a little different from the one on your computer.

Deploying a web app on PythonAnywhere involves pulling down your code from GitHub, and then configuring PythonAnywhere to recognise it and start serving it as a web application. There are manual ways of doing it, but PythonAnywhere provides a helper tool that will do it all for you. Let’s install it first:

{% filename %}PythonAnywhere command-line{% endfilename %}

  1. $ pip3.6 install --user pythonanywhere

That should print out some things like Collecting pythonanywhere, and eventually end with a line saying Successfully installed (...) pythonanywhere- (...).

Now we run the helper to automatically configure our app from GitHub. Type the following into the console on PythonAnywhere (don’t forget to use your GitHub username in place of <your-github-username>):

{% filename %}PythonAnywhere command-line{% endfilename %}

  1. $ pa_autoconfigure_django.py https://github.com/<your-github-username>/my-first-blog.git

As you watch that running, you’ll be able to see what it’s doing:

  • Downloading your code from GitHub
  • Creating a virtualenv on PythonAnywhere, just like the one on your own PC
  • Updating your settings file with some deployment settings
  • Setting up a database on PythonAnywhere using the manage.py migrate command
  • Setting up your static files (we’ll learn about these later)
  • And configuring PythonAnywhere to serve your web app via its API

On PythonAnywhere all those steps are automated, but they’re the same steps you would have to go through with any other server provider. The main thing to notice right now is that your database on PythonAnywhere is actually totally separate from your database on your own PC—that means it can have different posts and admin accounts.

As a result, just as we did on your own computer, we need to initialize the admin account with createsuperuser. PythonAnywhere has automatically activated your virtualenv for you, so all you need to do is run:

{% filename %}PythonAnywhere command-line{% endfilename %}

  1. (ola.pythonanywhere.com) $ python manage.py createsuperuser

Type in the details for your admin user. Best to use the same ones as you’re using on your own computer to avoid any confusion, unless you want to make the password on PythonAnywhere more secure.

Now, if you like, you can also take a look at your code on PythonAnywhere using ls:

{% filename %}PythonAnywhere command-line{% endfilename %}

  1. (ola.pythonanywhere.com) $ ls
  2. blog db.sqlite3 manage.py mysite static
  3. (ola.pythonanywhere.com) $ ls blog/
  4. __init__.py __pycache__ admin.py forms.py migrations models.py static
  5. templates tests.py urls.py views.py

You can also go to the “Files” tab and navigate around using PythonAnywhere’s built-in file browser.

You are now live!

Your site should now be live on the public Internet! Click through to the PythonAnywhere “Web” tab to get a link to it. You can share this with anyone you want :)

Note This is a beginners’ tutorial, and in deploying this site we’ve taken a few shortcuts which aren’t ideal from a security point of view. If and when you decide to build on this project, or start a new project, you should review the Django deployment checklist for some tips on securing your site.

Debugging tips

If you see an error while running the pa_autoconfigure_django.py script, here are a few common causes:

  • Forgetting to create your PythonAnywhere API token.
  • Making a mistake in your GitHub URL
  • If you see an error saying “Could not find your settings.py”, it’s probably because you didn’t manage to add all your files to Git, and/or you didn’t push them up to GitHub successfully. Have another look at the Git section above

If you see an error when you try to visit your site, the first place to look for some debugging info is in your error log. You’ll find a link to this on the PythonAnywhere Web tab. See if there are any error messages in there; the most recent ones are at the bottom.

There are also some general debugging tips on the PythonAnywhere help site.

And remember, your coach is here to help!

Check out your site!

The default page for your site should say “It worked!”, just like it does on your local computer. Try adding /admin/ to the end of the URL, and you’ll be taken to the admin site. Log in with the username and password, and you’ll see you can add new Posts on the server.

Once you have a few posts created, you can go back to your local setup (not PythonAnywhere). From here you should work on your local setup to make changes. This is a common workflow in web development – make changes locally, push those changes to GitHub, and pull your changes down to your live Web server. This allows you to work and experiment without breaking your live Web site. Pretty cool, huh?

Give yourself a HUGE pat on the back! Server deployments are one of the trickiest parts of web development and it often takes people several days before they get them working. But you’ve got your site live, on the real Internet, just like that!