4. Connecting the View to the Model

In the previous chapter, we created three models: Favorites, User, and Hyperlink. In this chapter, we’ll put those models to work.

Specifically, our models were set up to let us record the favorite Web sites, by URL, of users. We can record a URL in the Hyperlink model and a user in the User model and then record both the user and the user’s selected hyperlink in the Favorite model (from models.py, where you define your models in a Django application).

We created a Hyperlink model with one field, url:

class Hyperlink(models.Model):  url = models. URLField()

The User model is built into Django, so all we had to do was import it, like this:

from django.contrib.auth.models import User

The Favorite model lets ...

Get Django: Visual QuickPro Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.