Posted on Nov 9th, 2020
This week, we’ll be focusing on going further with Django before we dive into REST APIs.
Today, we’ll be talking about deploying our Django apps to Heroku and using PostgreSQL.
Assignments
We will continue to improve and finish Habit Tracker this week. Today, you’ll start by deploying it to Heroku.
Resources
Posted on Nov 4th, 2020
Today we talked about creating models and building views. We specifically went over the 5 big views used everywhere in Django.
The Five Views
# - list many models
def cohort_list(request):
cohorts = Cohort.objects.all()
return render(request, "core/cohort_list.html", {"cohorts": cohorts})
# - show one model
def cohort_detail(request, pk):
cohort = get_object_or_404(Cohort, pk=pk)
return render(request, "core/cohort_detail.html", {"cohort": cohort})
# - create a model
def cohort_create(request):
if request.method == "GET":
form = CohortForm()
else:
form = CohortForm(data=request.POST)
if form.is_valid():
cohort = form.save()
return redirect("cohort-detail", pk=cohort.pk)
return render(request, "core/cohort_create.html", {"form": form})
# - update a model
def cohort_update(request, pk):
cohort = get_object_or_404(Cohort, pk=pk)
if request.method == "GET":
form = CohortForm(instance=cohort)
else:
form = CohortForm(instance=cohort, data=request.POST)
if form.is_valid():
cohort = form.save()
return redirect("cohort-detail", pk=cohort.pk)
return render(request, "core/cohort_update.html", {"cohort": cohort, "form": form})
# - delete a model
def cohort_delete(request, pk):
cohort = get_object_or_404(Cohort, pk=pk)
if request.method == "POST":
cohort.delete()
return redirect("cohort-list")
return render(request, "core/cohort_delete.html", {"cohort": cohort})
Other Resources
Posted on Nov 2nd, 2020
Today, we will focus on understanding the database, how Django models interact with it, and how to model data for specific problems.
Assignment
This week, you will work on Habit Tracker. For Wednesday, you will want to have all your models created.
Data modeling problems for small groups
Small groups (use Google Meet or Zoom):
- Charlette, Nathan, Jon, Phil
- Kim, Derek, Babacar, Tom
Problem 1: You want to make an application for yourself to hold your favorite recipes. This application does not have users; since it’s just for you, there is no log in or registration. Recipes have ingredients and recipe steps. They also have “tags,” which allow you to put arbitrary categories on them like “breakfast”, “kid-friendly”, or “vegetarian.” Each recipe can have multiple tags; each tag can have multiple recipes. What’s the data model?
Problem 2: Take the above problem. You now want to make it so other people can use your application. Each person has their own recipes they own. Recipes can be public or private; private ones are only visible to the user who made it. What’s the new data model?
Problem 3: Take a simple version of Twitter. Users can make posts. Each post can have many images. Users can follow other users in order to see a list of posts from everyone they follow. Users can “like” posts to designate that they think it’s a great post. What’s the data model?
Problem 4: Take the above problem. You want to make it more like Facebook. Users don’t have a one-way follow; instead, they have “friends,” which is a two-way relationship. In addition, users can comment on their friends’ posts. What’s the data model?
Problem 5: Imagine a system for a library. The library has one or more copies of each book it owns. Users can check out individual copies of a book and the library needs to know who currently has the book. Books are due two weeks after check out. First, what questions do you need to ask to complete this data model? For these questions, decide on an answer. Then, what’s the data model?
Resources
Posted on Oct 30th, 2020
Welcome, all, to Phase 3! We are about to have a very exciting five weeks together.
We’re going to take the first week to strengthen our knowledge of Django with a set of lessons I call “Django from the Ground Up.” Now that you’ve gotten familiar enough with it to see it work together, we’ll break down how to use each piece effectively.
After that, you’ll move into your front-end and back-end classes.
Schedule
Week 1 (Nov 2-6)
- Lecture M, W, Th from 9:30-11:30 AM
- Small groups M, W, Th from 1-3 PM
- Check-in with Clinton, M, W, Th from 3:30-4 PM
- Tuesday: go vote, study hard
Weeks 2-5
In Phase 3 proper, we meet three days a week (M, Tu, Th). Front-end meets from 9:30-11:30 AM and back-end meets 2-4 PM. You will schedule two one-on-ones with me as well: Wednesday is reserved for those.
Your Zoom link: Clinton’s Classroom
Posted on Oct 26th, 2020
Post-video note
By the end of class we had gotten as far as getting a 403 request from our Django application.
The solution is to add the following to our AJAX request:
headers: { "x-csrftoken": form.elements["csrfmiddlewaretoken"].value },
This ensures that the token generated by csrf_token
is included in the request (when you
submit a form normally django knows how to handle it, but when you send an AJAX request this
needs to be specified).
Video Links