0

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:

  1. Django form to query database (models)
  2. Querying a django DB with model forms

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')),
]
11
  • 1
    You need to show what you tried and failed. Commented Aug 8, 2018 at 6:24
  • Separately, there doesn't seem to be a need for two models here; they have exactly the same fields. It would be better to have a single model - Incident, Report, or whatever - with a type field. Commented Aug 8, 2018 at 6:25
  • updated - this is what I currently have, just trying to search for the title as a temporary input field. - in regards to the two models. I have plans to merge the two, currently I have two tables within MySql. Commented Aug 8, 2018 at 7:30
  • And what happens with this code (apart from an error that char is not defined)? Commented Aug 8, 2018 at 7:36
  • the page reloads with "/?type=Slip+Hazard" appended to the url and nothing else that I can see. If char should throw an error does that mean query isnt getting set correctly then? Commented Aug 8, 2018 at 7:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.