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')),
]
  • 1
    You need to show what you tried and failed. – Daniel Roseman Aug 08 '18 at 06: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. – Daniel Roseman Aug 08 '18 at 06: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. –  Aug 08 '18 at 07:30
  • And what happens with this code (apart from an error that `char` is not defined)? – Daniel Roseman Aug 08 '18 at 07: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? –  Aug 08 '18 at 07:40
  • What do your URLs look like? Is "/qutheatmap" mapped to the search view? – Daniel Roseman Aug 08 '18 at 07:46
  • I had forgotten to add that, I just added it in. –  Aug 08 '18 at 07:53
  • But you have two identical patterns. You can't do that. Django will always call the first match, so the search view will never be called. – Daniel Roseman Aug 08 '18 at 07:57
  • so would I need to change the regular expression to match the "/?" for it to call that view –  Aug 08 '18 at 08:09
  • No, you need to pick a completely separate URL - for example "search" - and point your form action to that. (As another aside, don't include the full domain there as it won't work once you've deployed; just do `action="/qutheatmap/search/"` or whatever.) – Daniel Roseman Aug 08 '18 at 08:42
  • Doesn't that mean creating a new page also? I'd like to remain on the single page –  Aug 08 '18 at 08:47

0 Answers0