I have an issue with my Django forms and combining it with data from my database.
- I need to get data about tickets to generate the form via {% for ticket in tickets %}
- Once the user chose the ticket(s) and quantity, the form will check this request. I wanted to use Form.is_valid() and cleaned_data, however, I couldn't manage to combine this with step 1.
Do you guys have any tips or input how I can make my code more "safe"? Currently, I am skipping all the provided security Django provides with cleaned_data and is_valid(). The reason why is that I don't know how to do it.
views.py
from django.shortcuts import render
from .models import Ticket
from tickets.models import Order, Entry
# Create your views here.
def choose_ticket_and_quantity(request):
tickets = Ticket.objects.all()
if request.POST:
o = Order.objects.create()
request.session['order_id'] = o.order_id
ticket_id = request.POST.getlist('ticket_id')
ticket_quantity = request.POST.getlist('ticket_quantity')
for x in range(len(ticket_id)):
if int(ticket_quantity[x]) > 0:
e = Entry(
order=Order.objects.get(order_id = o.order_id),
ticket=Ticket.objects.get(id = ticket_id[x]),
quantity=ticket_quantity[x]
).save()
return render(request, "tickets/choose_ticket_and_quantity.html", {"tickets": tickets})
models.py
class Ticket(models.Model):
description = models.TextField()
name = models.CharField(max_length=120)
price_gross = models.DecimalField(max_digits=19, decimal_places=2)
quantity = models.IntegerField()
choose_ticket_and_quantity.html
<form action="" method="post">
{% csrf_token %}
{% for ticket in tickets %}
<input type="hidden" name="ticket_id" value="{{ ticket.id }}">
{{ ticket.name }}
<select name="ticket_quantity" >
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
{% endfor %}
<p><input type="submit" value="Checkout"></p>
</form>
Here what I currently tried/started within forms.py But I don't know how to render the *.html while getting the ticket data from its model.
from django import forms
#Currently not used
INT_CHOICES = [tuple([x,x]) for x in range(0,11)]
class TicketForm(forms.Form):
ticket_quantity = forms.IntegerField(widget=forms.Select(choices=INT_CHOICES))