forms.py
class AddAvailabilityForm(forms.Form):
def __init__(self, server, *args, **kwargs):
super(AddAvailabilityForm, self).__init__(*args, **kwargs)
self.fields['server'].queryset = server
server = forms.ModelChoiceField(queryset=Servers.objects.all(), empty_label=None, label=False)
availability = forms.IntegerField(label=False, widget=forms.TextInput(attrs={'class': 'form-input'}))
price = forms.IntegerField(label=False, widget=forms.TextInput(attrs={'class': 'form-input'}))
views.py
def addAvailability(request, slug):
games = Games.objects.get(id_num=slug)
server = Servers.objects.filter(id_num=slug)
if request.method == 'POST':
form = AddAvailabilityForm(request.POST)
if form.is_valid():
Availability.objects.create(**form.cleaned_data)
return redirect('base')
else:
form = AddAvailabilityForm(server)
context = {
'form': form,
'games': games
}
return render(request, 'Pay/sell_availability_detail.html', context)