Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

Connecting the View to the Model in Django

  • Print
  • + Share This
In the previous chapter, we created three models: Favorites, User, and Hyperlink. In this chapter, we'll put those models to work.
This chapter is from the book

This chapter is from the book

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 us store a user and a hyperlink—so, for example, if the user 'steve' listed USA Today as one of his favorite Web sites, we could record both the user and hyperlink in a Favorite object, which was defined like this in models.py:

class Favorite(models.Model):
  title =   models.CharField(max_length=200)
  user = models.ForeignKey(User)
  hyperlink =  models.ForeignKey(Hyperlink)

We set up the models in the previous chapter and filled the models with some data using the shell. In this chapter, we'll use those models, accessing the models from the view.

Does that mean that we have to create the models all over again and fill them with data a second time for the new chapter4 (as opposed to chapter3) project? Not at all. You can transfer databases between Django projects if you are careful about what you're doing, and you'll see how in this chapter.

After transferring the database, we'll access that database from the view in our new application: the favorites application in the chapter4 project. After all, the best model in the world is of no use to us if we can't access the data in that model.

So that's the plan in this chapter: create the chapter4 project, create the favorites application in it, transfer the database we created in the previous chapter, and then access the model's data from the new application's view (recall that in Django applications, the view is the brains of the application).

Creating the Project and Application

The first step is to create the chapter4 project and then create the favorites application inside that project. That's what we'll do in this task.

To create the Django project and application:

  1. To create the Django project, use django-admin.py. Open a command prompt and change to the directory that contains that application:
    $ cd Django-1.1\django\bin
    As discussed in previous chapters, you do not need to run django-admin.py from Django's bin directory. You can run this program from anywhere if you specify the path to it.
  2. Run django-admin.py, telling it you want to start a new project named chapter4:
    $ python django-admin.py
      startproject chapter4
    
    The django-admin.py program creates a new directory named chapter4 under the current directory.
  3. Change to the chapter4 directory:
    $ cd chapter4
  4. Run the manage.py program to create the new application named first like this:
    $ python manage.py startapp favorites
  5. Now you have to tell Django about your new application, favorites. Open settings.py in the chapter4 project's directory and find the INSTALLED_APPS section (Listing 4.1).

    Listing 4.1. The settings.py file.

    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
    )
    
  6. Add a line to tell Django about the chapter4.favorites application, shown in Listing 4.2.

    Listing 4.2. The edited settings.py file.

    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'chapter4.favorites',)
    
          
  7. Save the file.

Now we have a new project, named chapter4, and a new application, named favorites. The next step is to transfer the database file, favoritesdb, that we created so carefully in the previous chapter.

  • + Share This
  • 🔖 Save To Your Account