我试图查看用户何时单击“提交”按钮而不进行发布-它将显示错误消息,通知用户单击选项值。它给出了一个错误,指出“地点”处的MultikeyValue错误。当我选择时,它可以进行POST,但是我试图检索错误消息,以防用户未选择任何消息。
models.py
class Ticket(models.Model):
venue=models.CharField(max_length=100)
quantity=models.PositiveIntegerField()
price=models.DecimalField(default=25.00, max_digits=5, decimal_places=2, null=True, blank=True)
loop=models.CharField(max_length=100)
purchaser = models.ForeignKey(User, related_name="purchases", on_delete=models.PROTECT)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
dashboard.html
<form action="/add" method="POST">
{% csrf_token %}
<div class="text-center">
{% if messages %}
{% for message in messages %}
<div class="alert alert-danger p-2 pb-3">
<a class="close font-weight-normal initialism" data-dismiss="alert" href="#"><samp>×</samp></a>
{{message}}
</div>
{% endfor %}
{% endif %}
<label><strong>Location of Venue:</strong></label>
<select class="form-control" name="venue">
<option value="" selected disabled>Please select</option>
<option value="San Bernardino">San Bernardino</option>
<option value="Los Angeles">Los Angeles</option>
<option value="Riverside">Riverside</option>
</select>
<button type="submit" value="/add" class="btn btn-dark">Buy Tickets</button>
</form>
views.py
def add(request):
if not 'user_id' in request.session:
return redirect('/chrisgrafil')
if request.method!='POST':
return redirect ('/dashboard')
error=False
if len(request.POST['venue'])<1:
messages.error(request, "Please select venue")
error=True
if len(request.POST['quantity'])<1:
messages.error(request, "Please select quantity")
error=True
if len(request.POST['loop'])<1:
messages.error(request, "Please select loop")
error=True
else:
Ticket.objects.create(venue=request.POST['venue'], quantity=request.POST['quantity'], loop=request.POST['loop'], purchaser=User.objects.get(id=request.session['user_id']))
return redirect ('/confirmation')