I'm creating a website that shows available carparks in each college campus. How do I make the code insensitive to acronyms, e.g.g if the name of the campus is DCU Alpha?
carparks.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 %}
views.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)
Original Answer: String Manipulation
You can force uppercase strings in python / javascript like so:
python
javascript
EDIT: Conditional Formatting:
Let's assume we have campus names like so:
We can add some logic to check for campuses's with acrononyms:
views.py
We can also create two lists, one with campus names that have acronyms and one with campus names that do not: