{"id":14972774,"url":"https://github.com/jambonrose/python-web-dev-21-2","last_synced_at":"2025-09-20T19:31:39.086Z","repository":{"id":53668385,"uuid":"145429745","full_name":"jambonrose/python-web-dev-21-2","owner":"jambonrose","description":"Material for \"Web Development in Python with Django\" using Django 2.1, published as a Pearson LiveLesson on Safari Books Online","archived":false,"fork":false,"pushed_at":"2022-01-21T20:06:48.000Z","size":487,"stargazers_count":56,"open_issues_count":2,"forks_count":88,"subscribers_count":12,"default_branch":"class_material","last_synced_at":"2025-01-10T05:38:14.825Z","etag":null,"topics":["django","django-application","django-example","django-extensions","django-rest-framework","django-sample","heroku","herokuapp","python","python3","whitenoise"],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jambonrose.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-20T14:37:17.000Z","updated_at":"2024-12-10T13:37:00.000Z","dependencies_parsed_at":"2022-09-12T15:31:15.573Z","dependency_job_id":null,"html_url":"https://github.com/jambonrose/python-web-dev-21-2","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jambonrose%2Fpython-web-dev-21-2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jambonrose%2Fpython-web-dev-21-2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jambonrose%2Fpython-web-dev-21-2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jambonrose%2Fpython-web-dev-21-2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jambonrose","download_url":"https://codeload.github.com/jambonrose/python-web-dev-21-2/tar.gz/refs/heads/class_material","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233683413,"owners_count":18713725,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","django-application","django-example","django-extensions","django-rest-framework","django-sample","heroku","herokuapp","python","python3","whitenoise"],"created_at":"2024-09-24T13:47:30.573Z","updated_at":"2025-09-20T19:31:33.786Z","avatar_url":"https://github.com/jambonrose.png","language":"Jupyter Notebook","readme":"# Read Me\n\nThis repository contains the code for the second class in [Andrew\nPinkham]'s [Python Web Development] series, titled *Building Backend Web\nApplications and APIs with Django*. The series is published by Pearson\nand may be bought on [InformIT] or viewed on [Safari Books Online]. The\nseries is for intermediate programmers new to web development or Django.\n\n[Andrew Pinkham]: https://andrewsforge.com\n[Python Web Development]: https://pywebdev.com\n[InformIT]: https://pywebdev.com/buy-21-2/\n[Safari Books Online]: https://pywebdev.com/safari-21-2/\n\nAndrew may be reached at [JamBon Software] for consulting and training.\n\n[JamBon Software]: https://www.jambonsw.com\n\n## Table of Contents\n\n- [Changes Made Post-Recording](#changes-made-post-recording)\n- [Technical Requirements](#technical-requirements)\n- [Getting Started Instructions](#getting-started-instructions)\n  - [Docker Setup](#docker-setup)\n  - [Local Setup](#local-setup)\n- [Walking the Repository](#walking-the-Repository)\n- [Extra Problems](#extra-problems)\n- [Testing the Code](#testing-the-code)\n- [Deploying the Code](#deploying-the-code)\n\n## Changes Made Post-Recording\n\n1. For security purposes a commit has been added at the end of every\n   branch which secures the application using basic login functionality.\n   The content of that commit will be discussed in the third Python Web\n   Development class - I hope you're looking forward to it!\n2. Test dependencies have been included in all branches, so that the\n   Docker image may be built once and used on all branches.\n3. The Docker image has been updated to use Python 3.7.1 (from 3.7.0)\n4. Pre-Commit and Black have been updated to more recent versions\n5. The site uses Django's `StaticFilesStorage` instead of the\n   `ManifestStaticFilesStorage` shown in Lesson 7 by error.\n\n[🔝 Up to Table of Contents](#table-of-contents)\n\n## Technical Requirements\n\n- [Python] 3.6+ (with SQLite3 support)\n- [pip] 10+\n- a virtual environment (e.g.: [`venv`], [`virtualenvwrapper`])\n- Optional:\n  - [Docker] 17.12+ with [Docker-Compose] (or—if unavailable—[PostgreSQL] 10)\n\n\n[Python]: https://www.python.org/downloads/\n[pip]: https://pip.pypa.io/en/stable/installing/\n[`venv`]:https://docs.python.org/3/library/venv.html\n[`virtualenvwrapper`]: https://virtualenvwrapper.readthedocs.io/en/latest/install.html\n[Docker]: https://www.docker.com/get-started\n[Docker-Compose]: https://docs.docker.com/compose/\n[PostgreSQL]: https://www.postgresql.org/\n\nAll other technical requirements are installed by `pip` using the\nrequirement files included in the repository. This includes [Django 2.1].\n\n[Django 2.1]: https://docs.djangoproject.com/en/2.1/\n\n[🔝 Up to Table of Contents](#table-of-contents)\n\n## Getting Started Instructions\n\nFor a full guide to using this code please refer to Lesson 2 of the\nclass. This lesson demonstrates how to get started locally as well as\nhow to use the Docker setup.\n\nIf you are **unable to run Docker** on your machine skip to the [Local\nSetup](#local-setup) section.\n\n### Docker Setup\n\nThe use of Docker images allows us to avoid installing all of our\ndependencies—including PostgeSQL—locally. Furthermore, as discussed\nin the videos, it helps with parity between our development and\nproduction environments.\n\nOur Docker containers expect the existence of an environment file. To\ngenerate it on *nix systems please invoke the `build_docker_env.sh`\nscript.\n\n```shell\n./build_docker_env.sh\n```\n\nOn Windows please invoke the batch file.\n\n```\nbuild_docker_env\n```\n\nIf you run into problems please refer to the videos for why we use this\nand what is needed in the event these scripts do not work.\n\nTo run the Docker containers use the command below.\n\n```shell\ndocker-compose up\n```\n\nIf you wish to run the servers in the background use the `-d`\n(**d**etached) flag, as demonstrated below.\n\n```shell\ndocker-compose up -d\n```\n\nTo turn off the server use Control-C in the terminal window. If running\nin the background use the command below.\n\n```shell\ndocker-compose down\n```\n\nTo remove all of the assets created by Docker to run the server use the\ncommand below.\n\n```shell\ndocker-compose down --volumes --rmi local\n```\n\nThe `--volumes` flag may be shortened to `-v`.\n\n[🔝 Up to Table of Contents](#table-of-contents)\n\n### Local Setup\n\nUse `pip` to install your development dependencies.\n\n```console\n$ python3 -m pip install -r requirements/development.txt\n```\n\nIf you have checked out to an earlier part of the code note that you\nwill need to use `requirements.txt` instead of\n`requirements/development.txt`.\n\nYou will need to define the`SECRET_KEY` environment variable. If you\nwould like to use PostgreSQL locally you will need to set\n`DATABASE_URL`.\n\n```shell\nexport SECRET_KEY=`head -c 75 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 50`\n# replace the variables in \u003c\u003e below\nexport DATABASE_URL='postgres://\u003cUSER\u003e:\u003cPASSWORD\u003e@\u003cSERVER\u003e:5432/\u003cDB_NAME\u003e'\n```\n\nPlease be advised that if you are running code in Lesson 2 you should\nexpect to see errors. Lesson 2 changes the database structure but\navoids making migrations until the very last moment. What's more,\ndatabase settings change in Lesson 2.8. Errors are therefore normal!\n\n[🔝 Up to Table of Contents](#table-of-contents)\n\n## Walking the Repository\n\nTo make perusing the code in this repository as simple as possible the\nproject defines its own `.gitconfig` file with custom commands\n(aliases).\n\nTo enable the commands you must first point your local git\nconfiguration at the file provided. Either of the two commands below\nshould work.\n\n```shell\n# relative path\ngit config --local include.path \"../.gitconfig\"\n# absolute path - *nix only!\ngit config --local include.path \"`builtin pwd`/.gitconfig\"\n```\n\nThis will enable the following git commands:\n\n- `git next`: Move to the next example/commit\n- `git prev`: Move to the previous example/commit\n- `git ci`: shortcut for `git commit`\n- `git co`: shortcut for `git checkout`\n- `git st`: shortcut for `git status`\n- `git ll`: shortcut for `git log --oneline`\n\nThese commands can be used on any of the two branches in this\nrepository, which are listed below.\n\n- `class_material`: contains the code and material seen in the videos,\n  as well as solutions to exercises mentioned in Lessons 5 and 6 (see\n  section below to review).\n- `with_tests`: includes material above, as well as the tests used by\n  me to verify the code works\n\n[🔝 Up to Table of Contents](#table-of-contents)\n\n## Extra Problems\n\nAt the end of Lessons 5 and 6 I leave you with several optional\nexercises.\n\nIn Lesson 5:\n\n- Create new `NewsLink` objects in API via POST method (Consider\n  [`ViewSets`] vs [`APIView`] subclasses)\n- Simplify `PostSerializer` with [`HyperlinkedRelatedField`] (as seen on\n  `NewsLinkSerializer`)\n- Use [`ViewSets`] and Routers to simplify Post handling in API\n\n[`ViewSets`]: https://www.django-rest-framework.org/api-guide/viewsets/\n[`APIView`]: http://www.cdrf.co/3.7/rest_framework.views/APIView.html\n[`HyperlinkedRelatedField`]: https://www.django-rest-framework.org/api-guide/relations/#hyperlinkedrelatedfield\n\nIn Lesson 6:\n\n- Use [`CreateView`], [`UpdateView`], and [`DeleteView`] to create views\n  for `Startup` and `Post` objects (using `StartupForm` and `PostForm`)\n- Create a view to create new `NewsLink` objects and associate them\n  automatically with `Startup` objects\n- Expand this view to handle updating `NewsLink` objects\n- Allow for `NewsLink` objects to be deleted\n\n[`CreateView`]: http://ccbv.co.uk/projects/Django/2.0/django.views.generic.edit/CreateView/\n[`UpdateView`]: http://ccbv.co.uk/projects/Django/2.0/django.views.generic.edit/UpdateView/\n[`DeleteView`]: http://ccbv.co.uk/projects/Django/2.0/django.views.generic.edit/DeleteView/\n\n\nBelow are a few other tasks to test your knowledge.\n\n- Create links in the templates to enable easier navigation across the\n  site\n- Create a welcome page for the API (see the use of [`DefaultRouter`])\n\n[`DefaultRouter`]: https://www.django-rest-framework.org/api-guide/routers/#defaultrouter\n\nThe solutions to all of the tasks above can be found in the\n`class_material` git branch or [on Github].\n\n[on Github]: https://github.com/jambonrose/python-web-dev-21-2\n\n[🔝 Up to Table of Contents](#table-of-contents)\n\n## Testing the Code\n\nAll of the tests used to build the code can be found in the\n[`with_tests` branch] on Github.\n\n[`with_tests` branch]: https://github.com/jambonrose/python-web-dev-21-2/tree/with_tests\n\nThe branch (mostly) emulates a Test Driven-Development approach: commits\nprefixed with `test` write a test that will fail, while commits with\nlesson numbers then fix the failing tests from previous `test` commits.\n\nTo run the tests locally use `manage.py`.\n\n```shell\n# from root of project\ncd src\npython3 manage.py test\n```\n\nTests may also be run in Docker.\n\n```shell\n# from root of project\ndocker-compose run --rm django python manage.py test\n```\n\nBe advised that running tests in Lesson 2 is tricky. You will generally\nneed to create migrations before being able to run the tests, and there\nare a few commits that break the project's ability to run tests (such as\nwhen changing the database settings).\n\nWe will cover material about how to test Django in the next Python Web\nDevelopment class. I hope you're looking forward to it!\n\n[🔝 Up to Table of Contents](#table-of-contents)\n\n## Deploying the Code\n\nThe project follows [12 Factor App] principles and is configured to be\ndeployed to [Heroku].\n\nTo start you will need to [sign up for Heroku] unless you already have\nan account. Make sure you have installed the [Heroku CLI].\n\n[12 Factor App]: https://12factor.net/\n[Heroku]: https://www.heroku.com/\n[sign up for Heroku]: https://signup.heroku.com/\n[Heroku CLI]: https://devcenter.heroku.com/articles/heroku-cli#download-and-install\n\nThe following instructions are for *nix systems, and will need to be\nadapted for Windows.\n\nEnsure your app is ready for deployment.\n\n```shell\ndocker-compose run --rm django python manage.py check --deploy --settings=\"config.settings.production\"\n```\n\nFrom the command line create a new app.\n\n```shell\n$ heroku create\n```\n\nHeroku will give your app a random name (such as\n`infinite-tundra-77435`). Assign this name to a variable to be able to use\ncommands below.\n\n```shell\n$ export APP='infinite-tundra-77435' # replace with your actual app name\n```\n\nYour git repository should now have a new remote branch named `heroku`.\nIf it is missing you can add it manually.\n\n```shell\n$ heroku git:remote -a $APP\n```\n\nCreate a PostgreSQL database for your app.\n\n```shell\n$ heroku addons:create -a \"$APP\" heroku-postgresql:hobby-dev\n```\n\nConfigure your app to use production settings.\n\n```shell\n$ heroku config:set -a \"$APP\" DJANGO_SETTINGS_MODULE='config.settings.production'\n$ heroku config:set -a \"$APP\" SECRET_KEY=\"$(head -c 75 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 50)\"\n$ heroku config:set -a \"$APP\" PYTHONHASHSEED=random\n$ heroku config:set -a \"$APP\" WEB_CONCURRENCY=2\n```\n\nYou may now deploy your app.\n\n```shell\n$ git push heroku class_material:master\n$ # you can also deploy the other branch to Heroku using:\n$ git push heroku with_tests:master\n```\n\nTo create a new user use the command below.\n\n```shell\n$ heroku run -a $APP python src/manage.py createsuperuser\n```\n\nTo access the remote shell (to create data on the fly) use the command\nbelow.\n\n```shell\n$ heroku run -a $APP python src/manage.py shell_plus\n```\n\nTo see the app online you may use the command below.\n\n```shell\n$ heroku open -a $APP\n```\n\nFor more about what we've just done please see Heroku's [Getting\nStarted with Python] documentation. If Lesson 7 does not cover\nas much material as you'd like you may be interested in Chapter 29 of\n[Django Unleashed].\n\n[Getting Started with Python]: https://devcenter.heroku.com/articles/getting-started-with-python#introduction\n[Django Unleashed]: https://django-unleashed.com/\n\n[🔝 Up to Table of Contents](#table-of-contents)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjambonrose%2Fpython-web-dev-21-2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjambonrose%2Fpython-web-dev-21-2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjambonrose%2Fpython-web-dev-21-2/lists"}