Deployment & URL - koglak/SWE573 GitHub Wiki

Until now, website was only available on my computer. However, after we deployed it on internet, people will be able to see my new website.

We will use PythonAnywhere as a server provider and use Github as code hosting service. On the other hand, local computer will be used to develop and test codes.

Develop Website on Your Local Computer > Create a Copy on Github > Get Copy of Code from Github to PythonAnywhere.

1) .gitignore

There are some file that we don't prefer to commit. When we use git add ., we actually put all files to stage. Therefore, if we use .gitignore, we don't commit defined files anymore. Create a .gitignore file under your project and copy below codes.

# Python
*.pyc
*~
__pycache__

# Env
.env
myvenv/
venv/

# Database
db.sqlite3

# Static folder at project root
/static/

# macOS
._*
.DS_Store
.fseventsd
.Spotlight-V100

# Windows
Thumbs.db*
ehthumbs*.db
[Dd]esktop.ini
$RECYCLE.BIN/

# Visual Studio
.vscode/
.history/
*.code-workspace

After saved the file we should commit our project by using git.

git add .

git commit -m "adding .gitignore file"

git push -u origin main

2) PythonAnywhere

Click bash console. It is kind of cmd.

image

Later we should pull down project from Github to PythonAnywhere. Write below code to Bash console.

pip3.8 install --user pythonanywhere

Configure Github app by below code.

pa_autoconfigure_django.py --python=3.8 https://github.com/koglak/SWE573.git

It downloads code from Github, create virualenv on PythonAnywhere, update settings file by adding deployment settings, setting up database and configure the PythonAnywhere to serve new app via its API. All these staps are automated.

The database on PythonAnywhere is separate from database on local computer. Therefore, admin account should ne initialized.

It would be better to use same account with local computer.

 python manage.py createsuperuser

image

Now web site is ready on the public Internet.

This is easy deployment and contains vulnarablity from security point of view. For future projects: Deployment checklist

This is my new web on internet: Check My New Website!

image

If we go admin page and login with our account, we will see that we can still add posts, however, the post where we created on our local computer is not visible here! Because those are separate databases.

 https://koglak.pythonanywhere.com/admin

3) Django URLs

Go to mysite/url.py and edit the code as below. Main url will be the home page and show the posts.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]

Create a urls.py file under blog directory. First we import "path" and "views". Secondly, we add post_list view is selected as main page and URL named as post_list. It is important to define name for each URL in the project.

It is important to name each

 from django.urls import path
 from . import views

 urlpatterns = [
     path('', views.post_list, name='post_list'),
 ]

Go to your blog/views.py page and edit as below.

from django.shortcuts import render

def post_list(request):
    return render(request, 'blog/post_list.html', {})