我的add_player視圖需要一個team_season_id參數並顯示一個未綁定的窗體。Django窗體添加項目返回'NoneType'對象不可調用錯誤
def add_player(request, team_season_id):
team_season = get_object_or_404(TeamSeasonConference, pk=team_season_id)
player_form = PlayerForm()
return render_to_response('backoffice/organization/add_player.html', {
'player_form': player_form,
'team_season': team_season,
'player': None
}, context_instance=RequestContext(request))
這裏的回溯:
Traceback:
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/mypath/apps/account/decorators.py" in __call__
73. return self.view_func(request, *args, **kwargs)
File "/mypath/apps/backoffice/views.py" in add_player
877. player_form = PlayerForm()
File "/usr/lib/pymodules/python2.6/django/forms/models.py" in __init__
218. self.instance = opts.model()
Exception Type: TypeError at /backoffice/roster/add/player/2/
Exception Value: 'NoneType' object is not callable
這裏的PlayerForm:
class PlayerForm(forms.ModelForm):
CLASS_CHOICES = (
('', '-----'),
('FR', 'freshman'),
('RS-FR', 'red-shirt freshman'),
('SO', 'sophomore'),
('RS-SO', 'red-shirt sophomore'),
('JR', 'junior'),
('RS-JR', 'red-shirt junior'),
('SR', 'senior'),
('RS-SR', 'red-shirt senior'),
)
first_name = forms.CharField(max_length=100, required=False, label="First")
last_name = forms.CharField(max_length=100, label="Last")
jersey = forms.IntegerField(min_value=0, required=False, label="Jersey")
height = forms.CharField(max_length=10, required=False)
weight = forms.IntegerField(required=False, min_value=0)
age = forms.IntegerField(required=False, min_value=0)
year = forms.TypedChoiceField(choices=CLASS_CHOICES, required=False, empty_value='')
varsity_letters = forms.IntegerField(required=False, min_value=0)
position = forms.CharField(max_length=20, required=False)
hometown = forms.CharField(required=False)
major = forms.CharField(max_length=100, required=False)
freeform_school = forms.CharField(required=False, label="HS")
description = forms.CharField (widget=forms.Textarea, required=False)
HSSN = forms.CharField(max_length=8, required=False)
mugshot = forms.FileField(widget=forms.FileInput(), required=False)
team_season_connection = forms.ModelMultipleChoiceField(queryset=TeamSeasonConference.objects.filter(team = 1), required=False, widget=forms.SelectMultiple)
def save(self, commit=True, request=None, user=None):
player = super(PlayerForm, self).save(commit=False)
if commit:
# save the player
player.save()
return player
這裏的球員模型
class Player(models.Model):
CLASS_CHOICES = (
('FR', 'freshman'),
('RS-FR', 'red-shirt freshman'),
('SO', 'sophomore'),
('RS-SO', 'red-shirt sophomore'),
('JR', 'junior'),
('RS-JR', 'red-shirt junior'),
('SR', 'senior'),
('RS-SR', 'red-shirt senior'),
)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
suffix = models.CharField(max_length=5, blank=True)
nickname = models.CharField(max_length=100, blank=True)
slug = models.SlugField(max_length=200, blank=True, help_text="Used for associating content. (lowercase, hyphens sted spaces, no punctuation)")
team_season_connection = models.ManyToManyField(TeamSeasonConference, related_name='career_roster', blank=True)
team_season = models.ForeignKey(TeamSeasonConference, related_name='roster', blank=True)
height = models.CharField(max_length=10, blank=True)
weight = models.PositiveIntegerField(blank=True, null=True)
age = models.PositiveIntegerField(blank=True, null=True)
year = models.CharField(max_length=5, choices=CLASS_CHOICES, blank=True, db_index=True)
varsity_letters = models.PositiveIntegerField(blank=True, null=True)
position = models.CharField(max_length=20, blank=True)
jersey = models.PositiveIntegerField(blank=True, null=True)
mugshot = models.ForeignKey('content.Photo', null=True, blank=True)
profile = models.ForeignKey('content.Story', null=True, blank=True, help_text="Optionally link player to a profile story.")
major = models.CharField(max_length=100, blank=True)
freeform_school = models.CharField(max_length=100, blank=True, help_text="Take care to enter the school consistantly across players.")
hometown = models.CharField(max_length=100, blank=True, help_text="Town and state, i.e. Orlando, FL.")
offered_scholarship = models.BooleanField(help_text="Check if user has a scholarship offer.")
description = models.TextField(blank=True)
antagonist = models.BooleanField(help_text="To be used as indicator of whether this player is the season's antagonist")
pluck_user = models.ForeignKey ('auth.User', null=True, blank=True, help_text="Optionally link to user login")
# reverse generic relations
scrapbooks = generic.GenericRelation(Scrapbook)
不幸的是我們從這裏看不到'PlayerForm'的定義... –
對不起。增加了PlayerForm。 – wmfox3
哪個模型是'PlayerForm'應該是一個模型?我根本無法從代碼中看出來。 –