Reputation: 5
Hello I am creating a cinema book system in Django, and the user must select a film and its date/time from two drop down menus. My issue is that it only takes the film and not date/time. I was wondering how I would be able to add these two in the same form. Thanks
the html file:
<form action="/booking" method="post">
{% csrf_token %}
<div class="movie-container">
<label>Pick a film:</label>
<select name="selectedFilm">
{% for showing in showings %}
<option value="{{ showing.film }}" name="film">{{ showing.film }}</option>
{% endfor %}
</select>
<br>
<br>
<label>Pick a time:</label>
<select id="selectedDate">
{% for showing in showings %}
<option value="{{ showing.date_time }}" name="date">{{ showing.date_time }}</option>
{% endfor %}
</select>
<br>
<div class="btn-layer">
<input type="submit" value="Select">
</div>
</div>
</form>
the form file
class SelectedFilmForm(forms.Form):
selectedFilm = forms.CharField(required=True)
selectedDate = forms.DateTimeField(required=True, input_formats=["%Y-%m-%dT%H:%M", ])
enter code here
the models file
class Film(models.Model):
name = models.CharField(verbose_name='Film name', max_length=30)
age_rating = models.CharField(verbose_name='Age rating', max_length=5, choices=AgeRatings, default='U')
duration = models.DecimalField(verbose_name='Duration (minutes)', max_digits=4, decimal_places=1, blank=True, null=True)
description = models.CharField(verbose_name='Description', max_length=500, blank=True, null=True)
image = models.ImageField(upload_to='films',default='null')
def __str__(self):
return self.nameclass
Screen(models.Model):
screen_id = models.CharField(verbose_name='Screen ID', max_length=30)
capacity = models.IntegerField(verbose_name='Capacity')
def __str__(self):
return self.screen_id
class Showing(models.Model):
film = models.ForeignKey(Film, on_delete=models.CASCADE, verbose_name='Film', blank=True, null=True)
date_time = models.DateTimeField()
screen = models.ForeignKey(Screen, on_delete=models.CASCADE, verbose_name='Screen', blank=True, null=True)
def __str__(self):
return self.film.name
and my views file
def booking(request):
if request.method == "POST":
form = SelectedFilmForm(request.POST)
d = request.POST.get('selectedDate')
f = request.POST.get('selectedFilm')
print("Here", f)
print("Here", d)
if form.is_valid():
print(form.errors)
f = form.cleaned_data['selectedFilm']
d = form.cleaned_data['selectedDate']
print("Here", f)
print("Here", d)
# genderselect = form.cleaned_data['genderselect']
# request.session["genderselect"] = request.POST['genderselect']
request.session["selectedFilm"] = request.POST['selectedFilm']
request.session["selectedDate"] = request.POST['selectedDate']
else:
form = SelectedFilmForm()
film = request.session.get('selectedFilm')
date = request.session.get('selectedDate')
print("Here", film)
print("Here", date)
return render(request, "booking.html", {'film': film})
Upvotes: 0
Views: 216
Reputation: 5267
You haven't given your selectedDate dropdown a name. Note that your film dropdown has a name attribute and works. Try
<select id="selectedDate" name="selectedDate">
This will enable the selection to be pulled by your view.
Also, probably best to remove the name attribute in your options:
<option value="{{ showing.film }}" name="film"
It's not a suitable attribute for an option and may confuse things.
Upvotes: 1