我正在创建一个网站,显示每个大学校园的可用停车场。如果校园的名称是 DCU Alpha,如何使代码对首字母缩略词不敏感?
停车场.html
<h2>
{% if campus %}
{{ campus }}
{% else %}
No such campus
{% endif %}
</h2>
{% if campus %}
{% if carparks %}
<ul>
{% for carpark in carparks %}
<li>{{carpark.name}}: {{carpark.spaces}} spaces, {{carpark.disabled_spaces}} spaces for people with disabilities
<br>Spaces available: {{ carpark.spaces_available }}<br><br>
</li>
{% endfor %}
</ul>
{% else %}
<p>No carparks found</p>
{% endif %}
{% endif %}
视图.py
def carparks(request):
context = {}
Base_URL = 'http://jfoster.pythonanywhere.com/carparks/'
campus_name = request.GET['campus']
try:
campus = Campus.objects.get(name__iexact=campus_name)
except Campus.DoesNotExist:
return render(request,"parkatdcu/carparks.html",context)
carparks = Carpark.objects.filter(campus_id=campus)
carpark_info = []
for carpark in carparks:
URL = Base_URL + carpark.name
r = requests.get(URL).json()
if 'spaces_available' in r:
spaces_available = r['spaces_available']
else:
spaces_available = 'not available'
carpark_info.append({
'name': carpark.name,
'spaces': carpark.spaces,
'disabled_spaces': carpark.disabled_spaces,
'spaces_available': spaces_available
}
)
context['campus'] = campus_name.title()
context['carparks'] = carpark_info
return render(request,"parkatdcu/carparks.html",context)