I am building a site using Django framework with google api + MYSql. Currently I display all data entries' latitude and longitude from my dB onto a heatmap layer.
I need to be able to filter my data to refine what I am displaying. e.g - I would like to filter HazardInside.title by types such as "Slip" and "trip".
This filter will need to be able to filter by many other requirements simultaneously such as date and weather conditions. e.g title="Slip" + weather="Wet" + between dates of (dd/mm/yy - dd/mm/yy)
My current problem is successfully creating a view that requests new data from my dB and parses it to my HTML page.
models.py
class HazardInside(models.Model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=250)
incident_date = models.DateField(auto_now=False)
lat = models.FloatField(max_length=25, default=0.00000)
lng = models.FloatField(max_length=25, default=0.00000)
room_number = models.CharField(max_length=25)
floor = models.CharField(max_length=10)
def __unicode__(self):
return self.title
class InjuryInside(models.Model):
title = models.CharField(max_length=50)
description = models.CharField(max_length=250)
incident_date = models.DateField(auto_now=False)
lat = models.FloatField(max_length=25, default=0.00000)
lng = models.FloatField(max_length=25, default=0.00000)
room_number = models.CharField(max_length=25)
floor = models.CharField(max_length=10)
def __unicode__(self):
return self.title
views.py
from django.shortcuts import render, HttpResponse
from qutheatmap.models import Markers, HazardInside, InjuryInside
from django.template import RequestContext
from django.shortcuts import render_to_response
def home(request):
marker = Markers.objects.all()
hazardinsides = HazardInside.objects.all()
injuryinsides = InjuryInside.objects.all()
mapdata = {
'markers': marker,
'hazardinsides': hazardinsides,
'injuryinsides': injuryinsides
}
return render(request, 'heatmap/map.html', mapdata)
def search(request):
query = request.GET.get('type')
try:
query = char(query)
except ValueError:
query = None
hazardinsides = None
if query:
hazardinsides = HazardInside.objects.get(title=query)
context = RequestContext(request)
mapdata = {
'markers': marker,
'hazardinsides': hazardinsides,
'injuryinsides': injuryinsides
}
return render_to_response('heatmap/map.html', {"hazardinsides": hazardinsides,}, context_instance=context)
within my html
<form method="get" action="http://localhost:8000/qutheatmap/">
Search:<input type="text" name="type" id="id_q" value="{{ query }}"/>
<input type="submit" value="Search" />
</form>
I have tried a few different methods with my limited Django knowledge to no avail such as:
EDIT:
URLs
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home),
url(r'^$', views.search),
]
URLs config
from django.contrib import admin
from django.conf.urls import url, include
from qutheatmap import views
urlpatterns = [
url(r'qutheatmap/', include('qutheatmap.urls')),
]