I am building a section for my site that allows hunters to write hunting reports.
in this section they can choose from a 'wish list' (animals they would like to hunt) and 'bag list' (animals they have hunted)
I have built the form, and need to make use of some filters to extract only user related data.
I am having troubles with either my forms.py or something else due to the TemplateSyntaxError.
Basics of what I want is the 'bag list' to filter through my trophies only, and the user not having to select himself but to be already passed through, i.e. no select menu for the user.
my forms.py looks like this
class HuntingReportForm(ModelForm):
date_travel_started = forms.DateField(widget=extras.SelectDateWidget(years=range(1970,2012)))
date_travel_ended = forms.DateField(widget=extras.SelectDateWidget(years=range(1970,2012)))
wish_list = forms.ModelMultipleChoiceField(queryset=Specie.objects.all(), widget=FilteredSelectMultiple("verbose name", is_stacked=False), required=False)
bag_list = forms.ModelMultipleChoiceField(queryset=Trophies.objects.all(), widget=FilteredSelectMultiple("verbose name", is_stacked=False), required=False)
class Meta:
model = HuntingReport
#exclude = ['user',]
def __init__(self, user, *args, **kwargs):
#self.user = kwargs.pop('user', None)
self.validate = kwargs.pop('validate', False)
super(HuntingReportForm, self).__init__(*args, **kwargs)
self.fields['bag_list'] = forms.ModelMultipleChoiceField(queryset=Trophies.objects.filter(user = user), widget=FilteredSelectMultiple("verbose name", is_stacked=False), required=False)
users = User.objects.filter(userprofile__outfitter=True)
self.fields['outfitter'].choices = [('', '')] + [(user.pk, user.get_full_name()) for user in users]
self.fields.keyOrder = ['title', 'date_travel_started', 'date_travel_ended', 'outfitter', 'wish_list', 'bag_list','report', 'user']
your help is greatly appreciated.
You've overridden the signature of the form's __init__
method, by adding the user
arg first. You'll need to change everywhere you initialise the form to ensure you pass in user
before request.POST
.
Or, preferably, go back to the commented-out code you had which takes user
from the kwargs dict, and remove the reference to user
in the method signature.