This is your first Django project. For this project, functionality has already been added to this sample Django application. You will add new functionality throughout the project in order to better understand how all the pieces of Django work together.
You must have Poetry installed. This will allow you to get all dependencies of this project installed on your computer. You should already have Poetry installed, but if not, run the following command:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
Once you ensure you have Poetry installed, run the following command inside this project's directory:
poetry install
Poetry uses a concept called a "virtualenv" in order to isolate the dependencies of this project from any other project. This requires you to run a command when you want to start working on this project. Before working on the project, run:
poetry shell
This will change your terminal to use the virtualenv for this project. You must run this command in any new terminal you open. You will know if you are in the virtualenv because your prompt will change to show you. You should have the name of the virtualenv on your prompt, like the following:
django--uptact is 📦 v0.1.0 via 🐍 v3.7.7 (uptact-fsCrKkW6-py3.7)
❯
Note (uptact-fsCrKkW6-py3.7)
at the end -- that's the virtualenv. If you need to exit the virtualenv, simply close your terminal window or run exit
.
For the first assignment, spend time familiarizing yourself with Django. Look at the uptact
directory (the project directory) and the contacts
directory (an app directory). Answer the following questions for yourself:
- If I wanted to add a new URL to this project, what two files would I edit?
- If I wanted to add a birthday to each contact, what file would I edit?
Then do the following steps:
- Add a birthday field to the
Contact
model. This field should be of typeDateField
and should be allowed to be null and empty. - Make sure you can edit the birthday by changing the
ContactForm
. - Add the ability to display the birthday on the list of contacts. You will have to edit
templates/contacts/list_contacts.html
.
When you get through that, add a birthday to one of your contacts to test out your code.
With this assignment, we are going to explore relationships between models, and how URLs and views work.
Answer the following questions:
- If I wanted to add a new model, where would I do that?
- If I wanted to connect the new model to the
Contact
model, how would I do that?
Then do the following steps:
- Add a new model,
Note
, to thecontacts
app. This model should contain text for the note and the date/time of the note. Look at theauto_now_add
option for theDateTimeField
to have the date/time automatically populated. - Connect the
Note
model to theContact
model using aForeignKey
. - Use the Django console to add a note to one of your contacts.
- Make a new view and template to see an individual contact. The URL for this view should be
contacts/<int:pk>/
. Show the notes for that contact on this individual view. Otherwise, this page can look like an individual contact on the contacts list page.
With this assignment, we are going to explore forms.
Previously, you added a Note
model, but had no ability to create new notes through your Django application. Now do the following steps:
- Add a new form called
NoteForm
. This form should let you edit only one field, the text of the note. - Add a new view to accept this form via
POST
request and add a new note to a specific user. The user will be specified via the URL, which should becontacts/<int:pk>/notes/
. - On the individual contact view that you previously added, add a form to create new notes. When the note is created, redirect back to the contact view.
Test this by adding some notes to individual contacts.
This project was generated from the Momentum Django project template. This template sets up some minimal changes:
- django-extensions and django-debug-toolbar are both installed and set up.
- django-environ is set up and the
DEBUG
,SECRET_KEY
, andDATABASES
settings are set by this package. - There is a custom user model defined in
users.models.User
. - There is a
templates/
and astatic/
directory at the top level, both of which are set up to be used. - A
.gitignore
file is provided. - Poetry is used to manage dependencies.