tags:

views:

102

answers:

2

I'm new to Django and programming in general. I'm trying to make a simple site that allows players of a sport sign up for leagues that have been created by the admin. In my models.py, I created two models:

from django.db import models
from django.forms import ModelForm

class League(models.Model):
    league_name = models.CharField(max_length=100)
    pub_date = models.DateTimeField('date published')

class Info(models.Model):
    league = models.ManyToManyField(League)
    name = models.CharField(max_length=50)
    phone = models.IntegerField()
    email = models.EmailField()
    def __unicode__(self):
        return self.info

class InfoForm (ModelForm):
    class Meta:
        model = Info
        exclude = ('league')

From what I've read, I can probably use the Create/Update/Delete generic views to display a form for the user to sign up for the league. So with my app, I want the user to come to a simple homepage that lists the leagues, be able to click on the league and enter their info to sign up. Here's what my urlconf looks like:

from django.conf.urls.defaults import *
from mysite.player_info.models import League, Info, InfoForm

info_dict = {
    'queryset': League.objects.all(),
}

InfoForm = {'form_class' : InfoForm}

urlpatterns = patterns('',
    (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
    (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
    url(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='player_info/results.html'), 'league_results'),
    (r'^(?P<object_id>\d+)/info/create/$', 'django.views.generic.create_update.create_object', InfoForm),
)

Here's my problem: When I click on a league to sign up for on the homepage with my current setup, I get this error: TypeError at /league/1/info/create.... create_object() got an unexpected keyword argument 'object_id'. What am I doing wrong?

A: 

The issue isn't with your models, but rather with the function your "create" URL calls -- the line that calls django.views.generic.create_update.create_object() in urls.py. create_object() doesn't take an object_id argument, but you specified one in your url (r'^(?P<object_id>\d+)/info/create/$'). This makes sense -- you're creating an object, so you don't know its ID yet. create_object() only takes a form_class or model argument, as noted in the docs.

I'm guessing you're trying to create an Info object that is attached to a League object, and in that URL, <object_id> is the ID number of the League object; in which case, you shouldn't name that ID number, and instead should just use r"^\d+/info/create/$" as the URL. I'm not sure how you'll grab the league ID number using Django's create_object() function, though. You might have to write your own view handler. You may be able to use a custom ModelForm and pass it in with the form_class parameter, but I'm not sure.

mipadi
A: 

So would I need to completely scrap generic views or would writing some sort of wrapper be sufficient? Sorry this is probably a really novice question.

Ross