Skip to content

Instantly share code, notes, and snippets.

@debuggerpk
Created March 25, 2012 20:15
Show Gist options
  • Save debuggerpk/2199510 to your computer and use it in GitHub Desktop.
Save debuggerpk/2199510 to your computer and use it in GitHub Desktop.
'role' is an invalid keyword argument for this function
#models.py
class Player(models.Model):
first_name = models.CharField(max_length=32)
last_name = models.CharField(max_length=32)
team = models.ForeignKey(Team)
role = models.ManyToManyField(Roles)
preffered_position = models.IntegerField(max_length=3,choices=zip(range(1,12),range(1,12)) , default=1)
status = models.BooleanField(default=True)
#forms.py
class PlayerForm(forms.Form):
first_name = forms.CharField()
last_name = forms.CharField()
role = forms.ModelMultipleChoiceField(
Roles.objects.all(),
widget=CheckboxSelectMultiple
)
#views.py
@login_required
def add_player(request, team_id):
template = get_template('cricket/addplayer.html')
userteam = Team.objects.get(owner=request.user)
currentteam = Team.objects.get(id=team_id)
#now on to permissions .. if userteam = currentteam, the display form, otherwise display error
if userteam == currentteam:
if request.method == 'POST':
form = PlayerForm(request.POST)
if form.is_valid():
Player.objects.create(
first_name = form.cleaned_data['first_name'],
last_name = form.cleaned_data['last_name'],
team = Team.objects.get(id=team_id),
role = form.cleaned_data['role'],
)
return HttpResponseRedirect('/team/%s/' % team_id)
else:
form = PlayerForm
page_vars = Context({
'form': form,
'loggedinuser': loggedinuser,
'team': userteam,
})
crsfcontext = RequestContext(request, page_vars)
output = template.render(crsfcontext)
return HttpResponse(output)
else:
error = "Permission Denied: You cannot add player to " + str(currentteam.name)
page_vars = Context({
'loggedinuser': loggedinuser,
'error': error,
})
crsfcontext = RequestContext(request, page_vars)
output = template.render(crsfcontext)
return HttpResponse(output)
@debuggerpk
Copy link
Author

stack trace:

Environment:

Request Method: POST
Request URL: http://localhost/team/1/player/add/

Django Version: 1.3
Python Version: 2.7.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'djangocricket.Cricket',
'djangocricket.cms']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')

Traceback:
File "/usr/lib/pymodules/python2.7/django/core/handlers/base.py" in get_response

  1.                     response = callback(request, _callback_args, *_callback_kwargs)
    
    File "/usr/lib/pymodules/python2.7/django/contrib/auth/decorators.py" in _wrapped_view
  2.             return view_func(request, _args, *_kwargs)
    
    File "/home/yousuf/cricket/djangocricket/Cricket/views.py" in add_player
  3.                 role = form.cleaned_data['role'],
    
    File "/usr/lib/pymodules/python2.7/django/db/models/manager.py" in create
  4.     return self.get_query_set().create(**kwargs)
    
    File "/usr/lib/pymodules/python2.7/django/db/models/query.py" in create
  5.     obj = self.model(**kwargs)
    
    File "/usr/lib/pymodules/python2.7/django/db/models/base.py" in init
  6.             raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
    

Exception Type: TypeError at /team/1/player/add/
Exception Value: 'role' is an invalid keyword argument for this function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment