Wednesday, September 24, 2008

Django From the Ground Up: Episode 5

with host Eric Florenzano

Bookmark and Share

In this episode, we learn how to use Django's forms library (formerly referred to as newforms) to allow users of the site to be able to add new events to the system. We cover using ModelForms and how that allows us to tie a form instance to a model instance, and how it enables us to use only a subset of fields on the model to generate the form.

Url Reverse Resolution

We breezed through the portion of dynamically looking up a url based on a name, so I think it warrants some extra attention here. In the screencast, we used the django.core.urlresolvers.reverse method to get the 'tonight' page url, by passing it the name 'ev_tonight'. But surely there must be more to it--most urls have some sort of dynamic content that requires variables, etc. Luckily the signature on reverse() is quite simple. Its first argument is a string. This string can be either a name for a named url pattern (like we used above), or it can be a dot-separated python path to a view. For example, we could have used the string 'events.views.tonight' instead of 'ev_tonight'.

The rest of the arguments to reverse() are optional. One argument is args, which is typically a list of strings. These arguments are positional, and fill in the blanks of your urlpattern from left to right. These arguments don't make a distinction whether they are filling in named groups or standard positional groups. The second optional argument to reverse() is kwargs, which is a dictionary of named groups to strings which fill in those groups.

Lets give a few examples, given this url pattern:

url(r'^/event/(?P<username>\w+)/$', 'events.views.for_user', name='ev_for_user')

Any one of these calls to reverse() will result in the correct URL (/event/ericflo/):

reverse('ev_for_user', kwargs={'username': 'ericflo'})
reverse('ev_for_user', args=['ericflo'])
reverse('events.views.for_user', kwargs={'username': 'ericflo'})
reverse('events.views.for_user', args=['ericflo'])

As you can see, mixing and matching of different types of options works very well. One thing to note is that the optional keywords arguments are named 'args' and 'kwargs'. Many python programs use those as names for dynamic argument lists, but in this instance that is not the case.

Comments - 5 people have already said something. Join the discussion.

  • Fabio said

    It's amazing !!! I am currently studying Django and I hope these screens will help me.

    Thank you so much and congratulations for these screens!!

  • Björn said

    Just a quick tip if you're following along step-by-step:
    Testing the form will give you an error if you're not logged in. That might happen if you've shut down the testing server and have been working on something else. The easiest way to log in at this point is simply to go to the admin interface before trying to go to /event/create/

  • Eric Florenzano said

    @Björn Good catch! It's funny that you should mention it, too, because it's the first thing that I address in the next episode :)

  • nono said

    How about a ogg version of the screencasts?
    http://www.fsf.org/blogs/community/OggOnTheRise

  • Jacob said

    You Have to See This