Django project with Postgre, 2 html pages, and link between them.
Ensure that py command points to python3 with py -V (python 3 or above)
Check that pg 16+ is running on your machine with pg_ctl status (Windows), or pgrep -l postgres (Mac), or sudo systemctl status postgresql (Linux)
mkdir webplus && cd webplusAdd the following .gitignore
.venv
*.sqlite3
__pycache__git init
git add . && git commit -m "first commit"sudo -u postgres psqlCREATE DATABASE webplus;
CREATE USER webplususer WITH PASSWORD 'password';
ALTER ROLE webplususer SET client_encoding TO 'utf8';
ALTER ROLE webplususer SET default_transaction_isolation TO 'read committed';
ALTER ROLE webplususer SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE webplus TO webplususer;
\q
py -m venv .venv
source .venv/bin/activatepy -m pip install django psycopg2-binaryBelow, "core" means the core of our app, "." means build skeleton inside the current folder :
django-admin startproject core .
git add . && git commit -m "django first files"Inside settings.py, add localhost to the list of allowed hosts, like this :
ALLOWED_HOSTS = ['localhost', '127.0.0.1']And database settings like this :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'webplus',
'USER': 'webplususer',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}You can check changes with
git status
git diffThen commit changes
git add . && git commit -m "updated settings for db and allowed hosts"Django comes with default tables (to manage users and authorizations notably), so let's inject them into our new db :
py manage.py makemigrations
py manage.py migrateCreate a super user, just enter the following command and enter some realistic values when asked :
py manage.py createsuperuserCreate templates/home.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Home page</title>
</head>
<body>
<h1>Home</h1>
<p>I'm the home page. Go to <a href="/about">about</a> page.</p>
</body>
</html>Create templates/about.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>About page</title>
</head>
<body>
<h1>About</h1>
<p>It's about me. Back to <a href="/">home</a> page.</p>
</body>
</html>You can check & commit changes with
git status
git diff
git add . && git commit -m "added 2 HTML files"(Note that "git diff" don't display anything for new files. But VSCode can help).
Inside core/settings.py, update the 'DIRS' prop like this :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'], # <--- add 'templates' here
# ... other props
}
]Create core/views.py
from django.shortcuts import render
def homepage(request):
return render(request, 'home.html')
def aboutpage(request):
return render(request, 'about.html')Inside core/urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.homepage),
path('about', views.aboutpage),
]py manage.py runserverAnd navigate between home and about pages.