我正在用django框架和googleapi+mysql构建一个站点。目前,我把数据库中所有数据项的纬度和经度显示在热图图层上。
我需要能够过滤我的数据,以完善我的显示。e、 我想按“slip”和“trip”等类型过滤hazardinside.title。
这个过滤器将需要能够同时过滤许多其他要求,如日期和天气条件。e、 g title=“slip”+weather=“wet”+日期之间(dd/mm/yy-dd/mm/yy)
我当前的问题是成功地创建一个视图,该视图从我的db请求新数据并将其解析到我的html页面。
型号.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
视图.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)
在我的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>
我用我有限的django知识尝试了几种不同的方法,但没有效果,例如:
django表单查询数据库(模型)
用模型表单查询django数据库
编辑:
网址
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home),
url(r'^$', views.search),
]
URL配置
from django.contrib import admin
from django.conf.urls import url, include
from qutheatmap import views
urlpatterns = [
url(r'qutheatmap/', include('qutheatmap.urls')),
]
暂无答案!
目前还没有任何答案,快来回答吧!