Skip to content

Commit

Permalink
Django updates for PythonAnywhere (mdn#32246)
Browse files Browse the repository at this point in the history
* Django: Add Github to core part of development environment

* Move/reduce Git stuff in Deployment

* Railway - update reasons and make most stuff generic

* Add python anywhere and update Railway
  • Loading branch information
hamishwillee authored Feb 19, 2024
1 parent 4767ebf commit 7aece89
Show file tree
Hide file tree
Showing 24 changed files with 626 additions and 220 deletions.
654 changes: 443 additions & 211 deletions files/en-us/learn/server-side/django/deployment/index.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
162 changes: 162 additions & 0 deletions files/en-us/learn/server-side/django/development_environment/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,168 @@ py -3 -m django --version
> **Warning:** The rest of this **module** uses the _Linux_ command for invoking Python 3 (`python3`). If you're working on _Windows_ replace this prefix with: `py -3`
## Source code management with Git and GitHub
Source Code Management (SCM) and versioning tools allow you to reliably store and recover versions of your source code, try out changes, and share code between your experiments and "known good code" when you need to.
There are many different SCM tools, including git, Mercurial, Perforce, SVN (Subversion), CVS (Concurrent Versions System), etc., and cloud SCM hosting sources such as Bitbucket, GitHub, and GitLab.
For this tutorial we'll hosting our code on [GitHub](https://github.com/), one of the most popular cloud based source code hosting services, and using the **git** tool to manage our source code locally and send it to Github when needed.
> **Note:** Using SCM tools is good software development practice!
> Ths instructions provide a basic introduction to git and GitHub.
> To learn more, see [Learning Git](https://docs.github.com/en/get-started/quickstart/git-and-github-learning-resources).
### Key concepts
Git (and Github) use repositories ("repos") as the top level "bucket" for storing code, where each repo normally contains the source code for just one application or module.
Repositories can be public, in which case the code is visible to everyone on the internet, or private, in which case they are restricted to the owning organization or user account.
All work is done on a particular "branch" of code in your repo.
When you want to backup some changes to a branch you can create a "commit", which stores all changes since your last commit to the current branch.
The repo is created with a default branch named "main". You can spawn other branches off this using git, which initially have all the commits of the original branch.
You can evolve branches separately by adding commits, and then later on use a "Pull Request" (PR) on GitHub to merge changes from one branch to another.
You can also use git to switch between branches on your local compute, for example to try out different things.
In addition to branches, it is possible to create `tags` on any branch and later recover that branch at that point.
### Create an account and repository on GitHub
First we will create a free account on GitHub.
With a free account you can't create private repos, but you can create as many _public_ repositories ("repos") as you like.
Then we create and configure a repository named "django_local_library" for storing the [Local library website](/en-US/docs/Learn/Server-side/Django/Tutorial_local_library_website) as we evolve it in the rest of this tutorial.
The steps are:
1. Visit <https://github.com/> and create an account.
2. Once you are logged in, click the **+** link in the top toolbar and select **New repository**.
3. Fill in all the fields on this form.
While these are not compulsory, they are strongly recommended.
- Enter a repository name: "django_local_library".
- Enter a new repository description: "Local Library website written in Django".
- Select "Public" for the repository (the default).
> **Warning:** This will make _all_ source code visible.
> Remember not to store credentials or other sensitive material in your repo unless it is private.
- Choose **Python** in the _Add .gitignore_ selection list.
- Choose your preferred license in the _Add license_ selection list.
MDN uses "Creative Commons Zero v1.0 Universal" for this example.
- Check **Initialize this repository with a README**.
4. Press **Create repository**.
The repository will be created, containing just the files `README.txt` and `.gitignore`.
### Clone the repo to your local computer
Now that the repository ("repo") is created on GitHub we are going to want to clone (copy) it to our local computer:
1. On GitHub, click the green **Code** button.
In the "Clone" section, select the "HTTPS" tab, and copy the URL.
If you used the repository name "django_local_library", the URL should be something like: `https://github.com/<your_git_user_id>/django_local_library.git`.
2. Install _git_ for your local computer (you can find versions for different platforms [here](https://git-scm.com/downloads)).
3. Open a command prompt/terminal and clone your repo using the URL you copied above:
```bash
git clone https://github.com/<your_git_user_id>/django_local_library.git
```
This will create the repository inside the current directory.
4. Navigate into the repo folder.
```bash
cd django_local_library
```
### Modify and sync changes
Now we're going to modify the `.gitignore` file on the local computer, commit the change, and update the repository on GitHub.
This is a useful change to make, but mostly we're doing it to show you how to pull changes from GitHub, make changes locally, and then push them to GitHub.
1. In the command prompt/terminal we first "fetch" (get) and then pull (get and merge into the current branch) the latest version of the source from GitHub:
> **Note:** This step isn't strictly necessary as we have just cloned the source and know it is up to date.
> However in general you should update your sources from GitHub before making changes.
```bash
git fetch origin main
git pull origin main
```
The "origin" is a _remote_, which represents the location of the repo where the source is located, and "main" is the branch.
You can verify that origin is our repo on GitHub using the command: `git remote -v`.
2. Next we checkout a new branch to store our changes:
```bash
git checkout -b update_gitignore
```
The `checkout` command is used to switch some branch to be the current branch that you are working on.
The `-b` flag indicates that we intend to create a new branch named "update_gitignore" instead of selecting an existing branch with that name.
3. Open the **.gitignore** file, copy the following lines into the bottom of it, and then save:
```plain
# Text backup files
*.bak
# Database
*.sqlite3
```
Note that `.gitignore` is used to indicate files that should not be backed up by git automatically, such as temporary files and other build artifacts.
4. Use the `add` command to add all changed files (that aren't ignored by the **.gitignore** file) to the "staging area" for the current branch.
```bash
git add -A
```
5. Use the `status` command to check that all files you are about to `commit` are correct (you want to include source files, not binaries, temporary files etc.).
It should look a bit like the listing below.
```bash
> git status
On branch main
Your branch is up-to-date with 'origin/update_gitignore'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: .gitignore
```
6. When you're satisfied, `commit` the files to your local repo, using the `-m` flag to specify a concise but clear commit message.
This is equivalent to signing off on the changes and making them an official part of the local repo.
```bash
git commit -m ".gitignore: add .bak and .sqlite3"
```
7. At this point, the remote repo has not been changed.
We can push the `update_gitignore` branch to the "origin" repo (Github) using the following command:
```bash
git push origin update_gitignore
```
8. Go back to the page on GitHub where you created your repo and refresh the page.
A banner should appear with a button to press if you want to "Compare and pull request" the branch you just uploaded.
Select the button and then follow the instructions to create and then merge a pull request.
![Banner asking if user wants to compare and merge recent branch updates](github_compare_and_pull_banner.png)
After merging, the "main" branch on the repo on Github will contain your changes to `.gitignore`.
9. You can continue to update your local repo as files change using this add/commit/push cycle.
In the next topic we'll use this repo to store our local library website source code.
## Other Python tools
Experienced Python developers may install additional tools, such as linters (which help detect common errors in code).
Expand Down
30 changes: 21 additions & 9 deletions files/en-us/learn/server-side/django/skeleton_website/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ At the end of this article, we discuss other site-wide configuration you might a
To create the project:

1. Open a command shell (or a terminal window), and make sure you are in your [virtual environment](/en-US/docs/Learn/Server-side/Django/development_environment#using_a_virtual_environment).
2. Navigate to where you want to store your Django apps (make it somewhere easy to find like inside your _Documents_ folder), and create a folder for your new website (in this case: _django_projects_). Then change into your newly-created directory:

```bash
mkdir django_projects
cd django_projects
```

2. Navigate to the folder where you want to store your local library application.
This should be the folder named "django_local_library" that you [created as a local Github repository](/en-US/docs/Learn/Server-side/Django/development_environment#clone_the_repo_to_your_local_computer) when setting up the development environment.
3. Create the new project using the `django-admin startproject` command as shown, and then change into the project folder:

```bash
Expand All @@ -87,8 +82,8 @@ locallibrary/

Our current working directory should look something like this:

```python
../django_projects/locallibrary/
```bash
../django_local_library/locallibrary/
```

The _locallibrary_ project sub-folder is the entry point for the website:
Expand Down Expand Up @@ -348,6 +343,23 @@ At this point, we know that Django is working!

> **Note:** You should re-run migrations and re-test the site whenever you make significant changes. It doesn't take very long!
## Don't forget to backup to Github

We've just done some significant work, so now is a good time to backup the project.
You can use a similar set of commands to those in the [Modify and sync changes](/en-US/docs/Learn/Server-side/Django/development_environment#modify_and_sync_changes) section of the _Development environment_ topic:

```bash
git checkout -b skeleton_website # Create and activate a new branch "skeleton_website"
git add -A # Add all changed files to the staging area
git commit -m "Create Skeleton framework for LocalLibrary" # Commit the changed files
git push origin skeleton_website # Push the branch to GitHub
```

The create and merge a PR from your GitHub repo.
Note that if you don't delete the `skeleton_website` branch you can always switch back to it at some later point.

We won't necessarily mention this in future, but you may find it useful to create new branches at the end of each section in this tutorial.

## Challenge yourself

The **catalog/** directory contains files for the views, models, and other parts of the application. Open these files and inspect the boilerplate.
Expand Down

0 comments on commit 7aece89

Please sign in to comment.