README created by: Jay Lin
Website URL: http://prog5.azurewebsites.net/
This is a Django (Python-based framework) application that provides a map view of notes saved around the world. Users can write and save notes, then select whichever location on the world map they would like the note to be attached to.
- Python 3.10
- System with python may require
python3
orpython3.10
instead ofpython
when using commandline interface.
- System with python may require
- PostgreSQL header/library
- Ubuntu:
apt install libpq-dev
- Arch:
pacman -S postgresql-libs
- Ubuntu:
- Azure Database for PostgreSQL
- Set environment variables to pass credentials (see Quickstart #4)
- Optional
venv
(orvirtualenv
): Python Virtual environment is recommended to prevent dependency conflicts. See official document for more information.
- Packages in
requirements.txt
- setup.sh (to configure the secrets/env variables if testing locally)
- Clone this project
git clone https://github.com/jaeha-choi/CSS436_Program_5 && cd CSS436_Program_5
- (Optional) Create/Activate
venv
- Create
venv
in current directory if you haven't alreadypython -m venv venv
orvirtualenv ~/virt
- Activate
venv
- Linux/macOS (bash/zsh):
source venv/bin/activate
orsource ~/virt/bin/activate
- Windows (CMD):
venv\Scripts\activate.bat
- Windows (PS):
venv\Scripts\Activate.ps1
- Linux/macOS (bash/zsh):
- Verify
venv
- Linux/macOS:
where python
- Windows:
which python
- Linux/macOS:
- Create
- Install python package dependencies
pip install -r requirements.txt
- Set environment variables (without
<
,>
)# Azure Database PROJ_5_DB_HOST=<server_name>.postgres.database.azure.com PROJ_5_DB_USERNAME=<username> PROJ_5_DB_PASSWORD=<password> PROJ_5_DB_NAME=<db_name> # Django PROG_5_DJANGO_SECRET_KEY=<secret_key>
- Create database if you haven't
createdb -h <host> -U <user> <db_name>
- Run server
- Use default settings:
python manage.py runserver
- Use
0.0.0.0
as host and8080
as port:python manage.py runserver 0.0.0.0:8080
- Use default settings:
Type CTRL+C when you want to stop the local web server.
Below is a tree of the current project. Files of note have comments in parentheses next to their names. Files without comments you don't need to worry about on a basic level.
CSS436_Program_5
├── .gitignore
├── manage.py (command-line utility for administrative tasks)
├── mapnotes (houses our actual application)
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations (for migrating the data)
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py (where we setup the database schema)
│ ├── templates
│ │ └── mapnotes
│ │ ├── feed.html (list of all notes)
│ │ ├── index.html (main page showing the map)
│ │ └── user.html (shows 1 specific user)
│ ├── tests.py
│ ├── urls.py (specify app-specific URL locations)
│ └── views.py (how the pages' UI look)
├── mysite (configure project-wide settings)
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py (application settings, modules, databases...etc)
│ ├── urls.py (specify project-wide URL locations)
│ └── wsgi.py
├── Procfile
├── README.md
└── requirements.txt
Django static files include images, CSS, and Javascript files. For easy deployment, they can be gathered into a single
directory (project-wide) so they can be easily served. To do this and generate a staticfiles folder, specify STATIC_ROOT
= "some_path" in settings.py, then run python3 manage.py collectstatic
.
This application uses PostgreSQL (SQL or relational database).
- If models have changed:
- Run
python manage.py makemigrations mapnotes
to tell Django that we changed up our models in mapnotes/models.py (ex: need to create new tables). python manage.py migrate
to apply those changes.- Open interactive shell:
python manage.py shell
- See Django tutorial for more details.
- Run
- Connecting to PostgreSQL CLI
- Using
psql
- Default settings:
psql -h <host> -U <username> [db_name]
- With port specified:
psql -h <host> -p <port> -U <username> [db_name]
- Default settings:
- Using Azure CLI
- Install azure-cli via command line (ex:
brew install azure-cli
) - Login with your account using
az login
az postgres flexible-server connect -n postgresdemoserver -u dbuser -p "dbpassword" -d flexibleserverdb --interactive
Source: https://docs.microsoft.com/en-us/azure/postgresql/flexible-server/connect-azure-cli- This will start an interactive session in the Azure PostgreSQL server
- DROP DATABASE notes; CREATE DATABASE notes;
- Go back to your application and do python3 manage.py migrate. This will push the schema changes to Azure.
- Install azure-cli via command line (ex:
- Using
- Site ID: to check the list of sites, open a python shell and type:
from django.contrib.sites.models import Site
, thensorted([(site.id,site.name) for site in Site.objects.all()])
to view a list of all sites with IDs and their names.
See: https://devcenter.heroku.com/categories/working-with-django
heroku pg:psql
to connect to heroku postgres locally via CLI. You must have the heroku CLI installed.heroku run python manage.py shell
run python interactive on the live heroku serverheroku run python manage.py createsuperuser
create a super user on live heroku servergit push heroku local-branch-name:main
to push and deploy changes
- Parse JSON in script given a context: https://stackoverflow.com/questions/3345076django-parse-json-in-my-template-using-javascript