try:
hour = int(cleaned_data.get("hour"))
minute = int(cleaned_data.get("minute"))
datetime.time(hour = hour, minute = minute)
except (ValueError, TypeError):
raise django.forms.ValidationError("Invalid time")
=============================================================
view
=============================================================
CUST = grandmas4hire.models.Customer
APPT = grandmas4hire.models.Appointment
...
@django.contrib.auth.decorators.login_required
def admin_appt(request):
custs = CUST.objects.all()
custs = [(e.first + " " + e.last,
e.id) for e in custs]
appts = [e for e in APPT.objects.all()]
appts = sorted(appts,
key = lambda a : a.customer.last + a.customer.first + \
str(a.date) + str(a.time))
if request.method == "POST":
form = grandmas4hire.forms.AppointmentForm(request.POST)
if form.is_valid():
cust = form.cleaned_data["customer"]
cust = CUST.objects.get(id = cust)
year = int(form.cleaned_data["year"])
month = int(form.cleaned_data["month"])
day = int(form.cleaned_data["day"])
date = datetime.date(year = year,
month = month,
day = day)
hour = int(form.cleaned_data["hour"])
min_ = int(form.cleaned_data["minute"])
time = datetime.time(hour = hour, minute = min_)
if form.cleaned_data["am_or_pm"] == "PM":
time = datetime.time(hour = time.hour + 12,
minute = time.minute)
appt = APPT(customer = cust,
user = request.user,
date = date,
time = time,
status = "in process",
reminded = False,
updated = False)
appt.save()
reply = django.shortcuts.redirect("/admin_appt")
else:
reply = django.shortcuts.render(request,
"admin_appt.html",
{"form" : form,
"custs" : custs,
"appts" : appts})
else:
form = grandmas4hire.forms.AppointmentForm()
reply = django.shortcuts.render(request,
"admin_appt.html",
{"form" : form,
"custs" : custs,
"appts" : appts})
return reply
=============================================================
template
=============================================================
{% extends "html_base" %}
{% block body_elements %}
<div id = "admin_appt">
<form action = "." method = "post"> {% csrf_token %}
<p>ADD APPOINTMENTS {{form.errors}}</p>
<div>Customer:</div>
<p>
<select name = "customer" id = "id_customer">
{% for cust in custs %}
<option value = {{cust.1}}>
{{cust.0}}
</option>
{% endfor %}
</select>
</p>
<div>Date (YYYY-MM-DD):</div>
<p>{{form.year}}-{{form.month}}-{{form.day}}</p>
<div>Time (HH:MM): {{form.hour.errors}}</div>
<p>{{form.hour}}:{{form.minute}} {{form.am_or_pm}}</p>
<p><input type = "submit" value = "ADD APPOINTMENT"/></p>
</form>
<p><a href = "/admin">Go Back To Admin Page</a></p>
<table>
<tr>
<th>NAME</th>
<th>DATE</th>
<th>TIME</th>
</tr>
{% for appt in appts %}
<tr>
<td>
{{appt.customer.first}}
{{appt.customer.last}}
</td>
<td>{{appt.date|date:"Y-m-d"}}</td>
<td>{{appt.time|time:"h:i A"}}</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}