This project is based on Python Crash Course by Eric Matthes, third edition, from No Starch Press. Here is Eric's spec:
We’ll write a web app called Learning Log that allows users to log the topics they’re interested in and make journal entries as they learn about each topic. The Learning Log home page will describe the site and invite users to either register or log in. Once logged in, a user can create new topics, add new entries, and read and edit existing entries.
-
~/V$ python -m venv ll_env
This creates a virtual environment
~/V/ll_env
. -
source ~/V/ll_env/bin/activate
To activate it, execute
-
django-admin startproject ll_project .
This installs Django 5.0.3
-
django-admin startproject ll_project .
This starts the project and creates
settings.py
,urls.py
,wsgi.py
and a few other files. -
python manage.py migrate
This builds the database (SQLite) in
db.sqlite3
. -
python manage.py runserver
This starts the development server running on
localhost:8000
:Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). March 10, 2024 - 22:08:55 Django version 5.0.3, using settings 'll_project.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Keep the server running in its own terminal window, and in another, continue development.
-
python manage.py startapp learning_logs
This creates the app's infrastructure in a folder called
learning_logs
.
In models.py
, add a Topic class. In settings.py
in the project directory, add learning_logs
to the installed apps. Then run
python manage.py makemigrations learning_logs
Then apply the migration:
python manage.py migrate
Exexute python manage.py createsuperuser
and follow the prompts.
To register Topic with the admin site:
from .models import Topic
admin.site.register(Topic)
Edit models.py
, then run the familiar pattern:
python manage.py makemigrations learning_logs
python manage.py migrate
Then register Entry on the admin site by editing admin.py
.
The server notices the change, and the site updates automatically.
Execute python manage.py shell
to start an interactive shell.
Pages require URLs, views, and templates. URLs are defined by patterns which determine which page to return. The page is defined by a view, which may use a template. Some pages use forms to obtain user input.
Start by adding path('', include('learning_logs.urls')),
to the urls.py
file in the ll_project
directory. That causes any URL other than the admin ones to use the urls.py
file in the learning_logs
directory.
In learning_logs/urls.py
, the urlpatterns
list begins with the home page:
path('', views.index, name='index'),
The first arg matches the base URL (localhost:8000
for now). The second says to call the index
function in the views.py
module. The third gives this URL pattern a name.
The index function in views.py has the following code:
def index(request):
return render(request, 'learning_logs/index.html')
This passes the request
object and the 'learning_logs/index.html' template to the render
function.
Templates define types of pages. In this project, they inherit from base.html, which contains the following code:
<p>
<a href="{% url 'learning_logs:index' %}">Learning Log</a> -
<a href="{% url 'learning_logs:topics' %}">Topics</a>
</p>
{% block content %}{% endblock content %}
All pages have the same links, even when the link is to the page you're on. The base template can also have a single copy of company boilerplate.
The forms.py
module defines input forms for topics and entries. The forms are aware of the Topic
and Entry
classes defined in the models.py
module. The forms are classes that inherit from forms.ModelForm
, so they come with an is_valid
method that eliminates the need for a lot of ad hoc input validation code.
Templates make it easy to use the forms. The new_topic.html
and new_entry.html
templates display the forms, protect against cross-site request forgery, and display a submit button.