· 

Installing Django on PythonAnywhere (Part 4)

Coursera: Web Application Technologies and Django

[Started on March 2025]

https://www.youtube.com/watch?v=ZsEu-jyy2qY

https://www.pythonanywhere.com/

https://www.dj4e.com/assn/dj4e_install.md

 

➡ Read Part 1Part 2, and Part 3.

 

1. What have I done so far?

前回は、dj4e-samplesをgit cloneし、dependenciesをpip installし、python manage.py checkまで完了しました。今回はその続きです。

 

 

2.  https://www.dj4e.com/assn/dj4e_install.md

(1) このサイトのinstructionsにそってcommandsをrunしていきます。

 

Once the check works do:

python manage.py makemigrations

Then run:

python manage.py migrate

If you are doing this for the first time, it should run some migrations and create a file db.sqlite3.

 

この2つのcommandsについて、このあと確認します。

 

3. git status

しかし、その前に、前回、git cloneしているので、

git status

で、~/dj4e-samplesの現状を確認してみます。

Untracked filesとして、requirements52.txtが表示されました。これは、cloneのあと、私が作ったfileです。他人のGitHub accountからcloneした場合で、自分で作ったファイルがある場合、git add, git common までは、localへの変更なのでできそうですが、git pushはどうなるのか、よくわかりません。

 

4. git clone後のファイル追加

(1)そこで、ChatGPTに聞いてみました。

 

On django52, I created requirements52.txt, which was not in dj4e-samples.  Do I have to git add it and git commit?  What will happen if I git push it while I don't have permission to the account for dj4e-samples.

 

Great question — let’s break it down!


🗂️ 1. Should you git add and git commit requirements52.txt?

If you want to track your requirements52.txt file in your local Git repo (for example, to save your setup for later or share it), then:

bash
git add requirements52.txt
git commit -m "Add custom Django 5.2 requirements file"

🚫 2. What happens if you git push to a repo you don’t have permission to?

If you cloned the repo like this:

bash
git clone https://github.com/csev/dj4e-samples

Then you're cloning the original upstream repository, and you won’t have push access to that GitHub repo unless you're a collaborator.

If you run:

bash
git push

You’ll get an error like:

vbnet
remote: Permission to csev/dj4e-samples.git denied to your-username. fatal: unable to access 'https://github.com/csev/dj4e-samples.git/': The requested URL returned error: 403

✅ 3. How to safely make changes and push if needed

If you want to push changes to GitHub for your own use, you can:

Option 1: Fork the repo on GitHub

  1. Go to https://github.com/csev/dj4e-samples

  2. Click the Fork button

  3. Clone your own fork:

bash
git clone https://github.com/your-username/dj4e-samples

Now you can push to your fork:

bash
git add requirements52.txt
git commit -m "Add custom requirements file"
git push origin main

Option 2: Just keep the file locally

If you're the only one using the project, you don’t have to track it with Git — you can simply leave it in the folder and use it when needed.


Let me know if you want help forking the repo or pushing your changes to your own GitHub.

 

(2) ChatGPTの説明のとおり、まず、git add してみました。そのうえで、git statusで確認しました。

requirements52.txtがstagedされています。さらに、git commitです。これもうまくいくと思ったのですが、error messageが出ました。

どうやらこれは、virtual environmentに、GitHubの私のIDを登録していないためのようです。

 

5. git commitできない

(1) そこで、これについても、ChatGPTに聞いてみます。

 

On my virtual environment of django52, I tried to do git common.  But I got the following message.  What should I do?

(The error message is omitted.)

 

That message just means Git doesn't know who you are yet — you haven't told it your name and email, which it uses to tag commits.

You have two options depending on how you'd like to set it up:


Option 1: Set your name and email globally (recommended)

This sets it once for your whole system — every Git project you work on will use this info:

bash
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Example:

bash
git config --global user.name "Tom"
git config --global user.email "[email protected]"

Then try your git commit again and it should work!


🛠️ Option 2: Set it just for this project

If you want to keep your identity specific to this repo:

bash
git config user.name "Your Name"
git config user.email "[email protected]"

🔍 To check what you’ve set

You can confirm your config like this:

  • Global config:

    bash
    git config --global --list
  • Local (per-project) config:

    bash
    git config --list

 

Let me know if you want help choosing a GitHub username/email to match, or if you're using multiple GitHub accounts and want to manage them with SSH.

 

(2) ChatGPTの回答のOption 1により、user.nameとuser.emailを登録しました。

 

(3) 上で登録したものの確認はどうしたらよいでしょうか。ChatGPTに聞いてみます。

 

On my local system, I can do git commit.  Is there any way to know what email and name I set up for github?

 

Yes! You can check the Git username and email you've set on your local system by running these commands in your terminal or Git Bash:

 

🔍 To see your global Git identity:

 

git config --global user.name

git config --global user.email

 

🔍 To see your Git identity for the current repo (if you've set one locally):

 

git config user.name

git config user.email

 

🔧 Git uses the local config (set inside the repo) if it exists, otherwise it falls back to the global config.


💡 Want to see all your config settings?

bash
git config --list

This will show everything Git has configured, including user info, editor, line endings, etc.

 

Let me know if you want to change your settings or use different emails for different repos — totally doable!

 

(4) これらのcommandsを試してみました。

 

6. --global user.email & user.name

(1) 今、登録した私の情報は、どこに保存されているのでしょうか。ChatGPTで確認します。

 

Where is my global information stored locally?  I did git config --global user.email and user.name.  In what file can I find this global information?

 

Great question! Your global Git configuration (like user.name and user.email) is stored in a file called:

bash
~/.gitconfig

📍 Where to find it:

  • On Linux/macOS: It's in your home directory, so the full path is:

    arduino
    /home/your-username/.gitconfig

    or just:

    bash
    ~/.gitconfig
  • On Windows, it’s usually located at:

    makefile
    C:\Users\YourName\.gitconfig

📖 How to view it

You can open it with any text editor or just use the terminal:

bash
cat ~/.gitconfig

Or with an editor:

bash
nano ~/.gitconfig

You’ll see something like:

ini
[user] name = Your Name email = [email protected]

 

Let me know if you want to change it or set a different one for a specific repository!

 

(2) home directoryの.gitconfigというファイルの中に入っているとのことです。そこで、~に入って、catしてみました。

先ほど入力したemailとnameがそこにありました。

 

7. undo git commit

(1) 実は、その後、git commitに成功したのですが、もう一度、やり直してみます。それには、commitをundoする必要があるので、その方法をChatGPTに聞いてみます。

 

How can I undo git commit?

 

To undo a git commit, you have a few options depending on what you want to undo and whether you've pushed the commit yet.


✅ 1. If You Haven’t Pushed Yet

❌ Just want to undo the last commit (keep changes):

bash
git reset --soft HEAD~1
  • Removes the last commit.

  • Keeps your code changes in the staging area (ready to commit again).

❌ Undo the last commit (and unstage changes):

bash
git reset --mixed HEAD~1
  • Removes the last commit.

  • Puts the changes back into your working directory (unstaged).

❌ Undo the last commit completely (delete changes):

bash
git reset --hard HEAD~1

⚠️ This will permanently delete the last commit and any code changes.


🧨 2. If You Already Pushed It

You can still undo it, but be careful because it affects the remote:

bash
git reset --soft HEAD~1
git push --force

⚠️ Use this only if you're sure (and if you're working alone or your team is okay with force pushing).


Bonus: See recent commits

bash
git log --oneline

This helps you know exactly what you're undoing.


 

Let me know what your exact case is (e.g. "I committed by mistake but didn’t push yet"), and I’ll give you the safest step!

 

(2) まだpushしてない状態なので、

git reset --soft HEAD~1

でやってみます。

うまくいったようです。commitの前の状態、すなわち、stagedの状態に戻りました。

 

8. git commit

(1) 再度、git commitしてみます。

git commit -m "Add custom Django 5.2 requirements file"

をrunしてみます。

うまくいったようですが、commitの状態にあるか確認してみます。

GitHub: localでcommitしたけどpushがまだのfilesの確認方法 2025.03.23 を参照して、次のcommandを入力しました。

git log origin/main..HEAD --name-only

 

commit message とファイル名requirements52.txtが表示されました。すなわち、正常にcommitされています。

 

(2) この状態、すなわち、committedされた状態で、git pushしてみます。

他人のrepositoryなので拒絶されました。

 

9. fork

(1) 上記のGitHubの回答に、forkする方法があるとありました。そこで、forkしてみます。

<再掲>

If you want to push changes to GitHub for your own use, you can:

Option 1: Fork the repo on GitHub

  1. Go to https://github.com/csev/dj4e-samples

  2. Click the Fork button

  3. Clone your own fork:

bash
git clone https://github.com/your-username/dj4e-samples

Now you can push to your fork:

bash
git add requirements52.txt
git commit -m "Add custom requirements file"
git push origin main

そこで、まず、https://github.com/csev/dj4e-samplesへ行ってみます。fork buttonを押すと書いてありますが、そもそもfork buttonはどこにあるのでしょうか。

どうやら、Create a new forkが、ChatGPTのいうthe fork buttonのようです。そこで、これを押してみます。

右下にさらにCreate forkボタンが現れました。これを押します。

私のGitHubサイトに、dj4e-samplesが作られました。つまり、forkとは、他人のGitHubサイトのrepositoryを、まるごと、自分のGitHubサイトにコピーすることでした。

 

(2) ここで、mysite/dj4e-samplesからgit cloneすると、すでにlocalにあるdj4e-samples (csevからclone) したものはどうなるのでしょうか。しかも、localには、requirements52.txtという私が追加したfileもあります。ChatGPTに聞いてみます。

 

First, I git cloned csev/dj4e-samples.  I added my own file, requirements52.txt.  I staged it and committed it.  Then, I git pushed it only to fail, of course.  I forked csev/dj4e-samples, and now I have dj4e-samples on my GitHub site.  If I git clone mysite/dj4e-samples, what will happen with my local dj4e-samples repository, which was cloned from csev/dj4e-samples?  What will happen with requirements52.txt?  I fear it is erased because my local dj4e-samples repository is over-written.

 

Great question — and it’s smart of you to think ahead before cloning again. Let’s break it down:


🔍 Current Situation Recap:

  • You cloned from csev/dj4e-samples to your local machine (read-only).

  • You added and committed your own file requirements52.txt locally.

  • You couldn’t push because you don’t have write access to csev/dj4e-samples (expected).

  • Then you forked the repo to your own GitHub (yoursite/dj4e-samples).

  • Now you’re thinking of doing git clone yoursite/dj4e-samples.


🚫 What Happens If You Clone Again in the Same Folder?

If you clone again into the same folder, Git will stop you with an error like:

pgsql
fatal: destination path 'dj4e-samples' already exists and is not an empty directory.

But if you delete the folder first or clone into a new directory, then:

  • It creates a fresh copy of the repository from your GitHub.

  • Any local changes in the original folder (like requirements52.txt) will not be in the new clone unless you already pushed them to your forked repo.

  • So, yes — if you delete or overwrite the original dj4e-samples folder, requirements52.txt will be gone.


✅ Recommended Steps to Keep Your Work Safe:

  1. Back up your current file:

    bash
    cp dj4e-samples/requirements52.txt ~/requirements52.txt.backup
  2. Rename your current repo folder (if you want to keep it):

    bash
    mv dj4e-samples dj4e-samples-original
  3. Now you can safely clone from your fork:

    bash
    git clone https://github.com/yourusername/dj4e-samples
  4. Move your file into the new repo:

    bash
    mv ~/requirements52.txt.backup dj4e-samples/requirements52.txt
    cd dj4e-samples
    git add requirements52.txt
    git commit -m "Add requirements52.txt"
    git push origin main # or whatever branch you're working on

 

Let me know if you want to connect your original repo to your fork instead (by changing remotes) — that’s also an option.

 

ChatGPTがrecommendしたsteps、つまり、requirements52.txtのbackupをとって、forkに対してgit cloneしたのち、そうやってできたlocal repositoryにrequirements52.txtを入れる方法は、なんか冗長な気がします。それよりも、ChatGPTが最後のところで言った方法、つまり、connect your original repo to your forkの方がいいと思うので、これでやってみることにします。 

 

10. connect my original repo to my fork (by changing remotes)

 

(1) まずは、具体的な方法をどうするのか、ChatGPTに聞いてみます。

 

I want to connect my original repo to my fork instead (by changing remotes).  Can you tell me how to do that? 

 

Absolutely! You can connect your local repo (originally cloned from csev/dj4e-samples) to your own forked repo by updating the Git remote.

Here’s how to do it step-by-step:


✅ 1. Check your current remote

bash
git remote -v

You'll probably see something like:

perl
origin https://github.com/csev/dj4e-samples.git (fetch)
origin https://github.com/csev/dj4e-samples.git (push)

✅ 2. Change the origin remote to point to your fork

bash
git remote set-url origin https://github.com/YOUR_USERNAME/dj4e-samples.git

Replace YOUR_USERNAME with your actual GitHub username.


✅ 3. Confirm the change

bash
git remote -v

Now it should show:

bash
origin https://github.com/YOUR_USERNAME/dj4e-samples.git (fetch)
origin https://github.com/YOUR_USERNAME/dj4e-samples.git (push)

✅ 4. Now you can push!

bash
git push origin main

(Use main or whatever branch you're working on)


 

Let me know if you'd also like to keep the original repo as a separate remote (e.g., upstream) for future updates from the source!

 

(2) そこで、このとおりにやってみます。まずは、

git remote -v

で、現在のremoteつまりoriginの確認です。

 

fetch もpush も、originは、/csev/dj4e-samplesになっており、これは最初のclone元フォルダです。

次に、

git remote set-url origin https://github.com/mysite/dj4e-samples

により、remoteのurlを私のサイトに変更します。

これにより、originが私のサイト上にあるdj4e-samplesに変更されたはずです。それを確認するために、再度、

git remote -v

を入力します。

先ほどは、csevだったところが、mysiteに変わっています。これで、pushできるはずです。まずは、committed状態になっているかの確認です。

git log origin/main..HEAD --name-only

を入力してみます。

requiremts52.txtがcommitされた状態になっていることがわかりました。この状態で、

git push origin main

してみます。今度は、自分のサイトへのpushなので、うまくいくはずです。

user nameとpasswordを聞いてきましたので、それを打ちます。passwordについては、入力しても、表示されないようになっていました。そして、enter keyを押しました。ところが、

表示のように、error messageが出ました。その理由は、remoteにすでに変更箇所があるため、先にgit pullでその変更をlocalに落としてから、pushせよとのことでした。簡単にできると思って、この方法を取ったのでしたが、思わぬ落とし穴があったので、結局、この方法はやめることにしました。

 

11. Recommended Steps to Keep Your Work Safe

 

(1) そこで、ChatGPTがrecommendした方法に戻ることにしました。その方法を再掲します。

 

✅ Recommended Steps to Keep Your Work Safe:

  1. Back up your current file:

    bash
    cp dj4e-samples/requirements52.txt ~/requirements52.txt.backup
  2. Rename your current repo folder (if you want to keep it):

    bash
    mv dj4e-samples dj4e-samples-original
  3. Now you can safely clone from your fork:

    bash
    git clone https://github.com/yourusername/dj4e-samples
  4. Move your file into the new repo:

    bash
    mv ~/requirements52.txt.backup dj4e-samples/requirements52.txt
    cd dj4e-samples
    git add requirements52.txt
    git commit -m "Add requirements52.txt"
    git push origin main # or whatever branch you're working on

(2) まずは、requirements52.txtのbackupを取って、別の場所に保管します。

cp dj4e-samples/requirements52.txt ~/requirements52.txt.backup

home directoryに保管し、確認のため、lsで見ておきました。ちゃんとありました。

 

(3) 次に、今あるlocal repository、すなわち、csevからgit cloneしてrequirements52.txtを追加したrepositoryの名前を変えます。copyを取るときは、cpですが、ファイル名変更は、mvを使います。

mv dj4e-samples dj4e-samples-original

これにより、csevからcloneしたdj4e-samplesの名前を、dj4e-samples-originalに変更します。

mvで名前変更した後、lsで、確認しました。dj4e-samples-originalというfile nameに変更されていました。

ところで、先ほど行った作業の中で、

git remote set-url origin https://github.com/mysite/dj4e-samples

により、remoteのURLを/mysite/dj4e-samplesにしていたので、これをもとに戻す必要があります。

mvでファイル名変更したので、dj4e-samples-originalへcdしたあと、次の作業をしました。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

 

すなわち、git remote -vで現在のoriginを確認したのち、git remote set-url...でorigin変更し、再度、git remote -vでoriginを確認しました。その結果、originはもとどおりの/csev/dj4e-samplesに戻すことができました。

 

(4) mysite上にあるdj4e-samplesを、git cloneします。まず、home directoryへcd して、lsにより、そこにdj4e-samplesと同名のフォルダがないことを確認します。そして、

git clone https://github.com/mysite/dj4e-samples

により、/mysite/dj4e-samplesをcloneします。

 

うまくいきました。home directoryに、dj4e-samplesが作られています。

 

(5) backupしておいたrequirements52.txtをlocalのdj4e-samplesの中に入れます。mv...により、移動させます。

localのdj4e-samplesの中に、requirements52.txtが入りました。

 

(6) さて、ここまでの作業は、git cloneしたlocal repoにrequirements52.txtを追加した、というものです。このあと、requirements52.txtをgit add して、commitして、さらにpushします。まず、

git status

で、requirements52.txtがuntrackedであることを確認します。

次に、git add します。git statusで確認もします。

さらに、git commit します。git logで確認もします。

次は、git pushです。

うまくいったようです。確認のため、私のGitHub siteのdj4e-samplesを見てみます。

requirements52.txtが追加されていました。forkへのpushは成功です。

 

12. pushの際のpassword

 

(1) 上記のpushのときはすでに解決していた問題なのですが、git pushするときに、usernameとpasswordを求められるのですが、passwordについては、通常のpasswordをいくら入力しても拒絶されるというのが続いていました。これについてのChatGPTとの会話を紹介します。

 

Why did this happen?

(django52) 15:35 ~/dj4e-samples (main)$ git push origin main

Username for 'https://github.com': [myname]

Password for 'https://[mymail]@github.com':[I entered my password]

remote: Support for password authentication was removed on August 13, 2021.

remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-http

s-urls for information on currently recommended modes of authentication.

Great question — and you're very close! Here's why that happened:

❗ What happened?

GitHub no longer allows password authentication for Git operations over HTTPS (since August 13, 2021). That means when you enter your GitHub username and password, it will fail — even if your credentials are correct.

 

✅ What should you use instead?

You have two options:

🔐 Option 1: Use a Personal Access Token (PAT)

Instead of your GitHub password, you generate a token from GitHub and use that as your "password."

Steps:

  1. Go to:
    👉 https://github.com/settings/tokens

  2. Click "Generate new token (classic)"
    (or "Fine-grained token" if you're familiar with scopes)

  3. Set scopes like:

    • repo (to access your repos)

    • Give it a name and expiration (e.g. 90 days)

  4. Copy the token (it shows only once).

  5. When Git asks for a password, paste the token instead.

💡 Optional: Cache your credentials

So you don’t have to enter the token every time:

bash
git config --global credential.helper cache

Or store them more permanently:

bash
git config --global credential.helper store

🔐 Option 2: Use SSH authentication

This is a longer setup, but you only need to do it once. SSH is secure and avoids tokens altogether.

Would you like help setting up SSH keys?

 

Let me know which option you'd prefer (PAT or SSH), and I’ll walk you through it!

 

つまり、HTTPSを介した作業(たとえば、git pushなど)は、通常のpasswordでは対応してくれなくて、代わりに、tokenやSSHが必要になるとのことでした。

 

(2) そこで、tokenを使うべく、ChatGPTのinstructionsにしたがって、まず、

https://github.com/settings/tokens

へいってみました。

次に、右の方にある Generate new token ボタンを押しました。

2つ選択肢が現れるので、Classicの方を選んでさらにclickすると、passwordを求められるので、GitHubのログインパスワードを入力します。

入力画面が現れます。

私は、Noteのところに、何のtokenか入力(to access my repos)して、Expirationは90 daysくらいにして、repoのところだけにcheckを入れました。

そして、画面のずっと下の方にある Generate token ボタンを押すと、tokenが生成されます。

生成されたtokenは二度と表示されないとのことなので、copyして、どこかに保存しておきます。

 

これで、tokenが使えるようになりました。今後は、git pushの際に、passwordを求められたら、tokenをそこに入力(貼り付け)すればよいことになります。tokenの寿命については、

https://github.com/settings/tokens

に表示されています。

 

(3) Optional: Cache your credentials

 

ChatGPTの回答の後半にある、

git config --global credential.helper cache

のところが、何をしたらよいのかわからないので、再度、ChatGPTに聞いてみました。

 

I don't understand how to store my token in a cache.  You showed me:

git config --global credential.helper cache

I entered this command.  But nothing happened.  I don't know what to do when entering my token for git push.  I don't know what credential.helper is.  Can you explain what to do step by step?

 

Absolutely! Let's break it down step by step. You’re not alone—this part of Git can be confusing, especially since GitHub removed password authentication and moved to personal access tokens (PATs).


✅ What is credential.helper?

credential.helper is a Git setting that tells Git how to remember your credentials so you don’t have to enter them every time you do a git push, pull, or fetch.

  • cache: Git will temporarily remember your token for 15 minutes (by default).

  • store: Git will remember your token permanently in a file on disk (not encrypted, so not recommended).

  • manager or osxkeychain: These store tokens securely on Windows or macOS, respectively.

You entered:

bash
git config --global credential.helper cache

That’s good! You told Git to cache your credentials (but nothing happens right away—this command just sets the option).


💡 Step-by-step: Using a token with Git

1. ✅ Set credential helper (you’ve already done this):

bash
git config --global credential.helper cache

2. ✅ Make sure you're pushing using HTTPS (not SSH)

Run this in your repo to confirm:

bash
git remote -v

It should look like:

perl
origin https://github.com/yourusername/your-repo.git (fetch)
origin https://github.com/yourusername/your-repo.git (push)

3. ✅ Try pushing:

bash
git push origin main

When prompted:

  • Username: Enter your GitHub username

  • Password: Paste your Personal Access Token (PAT) (not your GitHub password!)

🧠 Git will not show anything as you paste the token (no *, no text, just invisible input)—this is normal!

4. ✅ Git now caches that token

Now for the next 15 minutes, Git remembers your token so you won’t be asked again.


🔐 Want to cache your token longer?

You can set a timeout like this (e.g. 1 hour = 3600 seconds):

bash
git config --global credential.helper 'cache --timeout=3600'

✅ What if you want Git to remember it permanently (not recommended unless it's a personal laptop)?

You can use:

bash
git config --global credential.helper store

Then the next time you enter your username and token, Git saves it in plain text in a file like this:

bash
~/.git-credentials

You can view it with:

bash
cat ~/.git-credentials

Let me know if you want to switch to SSH instead (you won't need to enter tokens that way). Or if you'd like to see how to generate a GitHub token.

 

すなわち、

git config --global credential.helper cache

は、tokenを入力後15分間はそれをsystemに記憶させる設定をするcommandで、passwordを求められてtokenを入力すると、tokenがcacheとして15分間保たれるとのことです。また、

git config --global credential.helper store

は、~/.git-credentialsにtokenそのまま記録するcommandですが、危険なのでChatGPTは推奨していません。

 

(4) それでは、cacheへの記録もしたくない場合はどうしたらよいでしょうか。ChatGPTに聞いてみます。

 

How can I remove chache-remembering?  How can I set the system not to remember my token?

 

Great question! If you want Git to stop remembering your token, you just need to unset or remove the credential helper you previously set.

Here’s how to do that:


🧹 Step 1: Remove the cached credential helper

If you set:

bash
git config --global credential.helper cache

To remove it, run:

bash
git config --global --unset credential.helper

🧹 Step 2: If you used store instead of cache (permanent)

If you previously used:

bash
git config --global credential.helper store

And Git saved your token in a file like ~/.git-credentials, remove the helper and the file:

bash
git config --global --unset credential.helper
rm ~/.git-credentials

🧹 Step 3: Check if it's cleared

To confirm you have no credential helpers active:

bash
git config --global --get credential.helper

If nothing is returned, then no credential helper is set, and Git will ask for your token every time.


 

Let me know if you'd like to switch to a secure helper (like a password manager or SSH)!

 

So much for today.

Next, Part 5.