Form
class StadiumForm(forms.Form):
location = forms.BooleanField()
def __init__(self, locId, *args, **kwargs):
super(StadiumForm, self).__init__(*args, **kwargs)
self.fields['location'].choices = locId
views
def search_results(request):
if request.method == 'POST':
FORMS_COUNT = 5
date_form = DateForm(request.POST)
team_forms = [TeamForm(request.POST, prefix="team%d" %(x)) for x in range(FORMS_COUNT)]
showResults = True
stadium_forms = []
if date_form.is_valid():
games = False
for tf in team_forms:
if tf.is_valid():
searchTeam = Team.objects.get(name = tf.cleaned_data['name'])
# Using a neat trick to chain together multiple filter sets because we don't always
# know how many teams we will be searching for
if games == False:
if tf.cleaned_data['homeOrAway'] == u'Home':
games = Game.objects.filter(home_team = searchTeam, game_date__gte = date_form.cleaned_data['startDate']).order_by('game_date', 'start_time') & Game.objects.filter(home_team = searchTeam, game_date__lte = date_form.cleaned_data['endDate']).order_by('game_date', 'start_time')
else:
games = Game.objects.filter(away_team = searchTeam, game_date__gte = date_form.cleaned_data['startDate']).order_by('game_date', 'start_time') & Game.objects.filter(away_team = searchTeam, game_date__lte = date_form.cleaned_data['endDate']).order_by('game_date', 'start_time')
else:
if tf.cleaned_data['homeOrAway'] == u'Home':
games = Game.objects.filter(home_team = searchTeam, game_date__gte = date_form.cleaned_data['startDate']).order_by('game_date', 'start_time') & Game.objects.filter(home_team = searchTeam, game_date__lte = date_form.cleaned_data['endDate']).order_by('game_date', 'start_time') | games
else:
games = Game.objects.filter(away_team = searchTeam, game_date__gte = date_form.cleaned_data['startDate']).order_by('game_date', 'start_time') & Game.objects.filter(away_team = searchTeam, game_date__lte = date_form.cleaned_data['endDate']).order_by('game_date', 'start_time') | games
for game in games:
stadium_forms.insert(int(game.location.id), StadiumForm(int(game.location.id), prefix=int(game.location.id)))
else:
games = 'Hoopa'
return render_to_response('search/results.html', {'games': games, 'showResults': showResults, 'stadium_forms': stadium_forms})
else:
return HttpResponseRedirect('/')
template
This is where the search results would go
{% if showResults %}