1
我試圖讓應用程序讓用戶訪問網站,使用位置數據提交表單以縮小數據庫條目,然後根據該信息顯示情節。用戶只需訪問索引頁面,點擊提交按鈕即可在表單下方顯示圖表。在內部,模板調用了圖表視圖,該視圖使用來自請求的信息來過濾數據並返回.png。在我的指數模板,我有:將Django表單數據發送到模板中的URL
<form action='.' method="get">
State: <input type="text" name="State">
ZIP: <input type="text" name="Zip">
City: <input type="text" name="City">
<input type="submit" value="submit">
</form>
<img src='plot.png?State={{ request.GET.State }}&Zip={{ request.GET.Zip }}&City={{ request.GET.City }}'>
如果我硬編碼的標籤,就像這樣:
<img src='plot.png?State=FL&Zip=&City=MIAMI'>
它工作得很好,但我不能讓形式從發送信息它的請求立即在它的標籤下。我可以改爲將圖形指向繪圖視圖本身,但是該頁面會將您帶到不想要的圖像。我將如何去解決這個問題?
編輯: 對不起,沒有包括urls.py和views.py,他們在這裏!
urls.py
urlpatterns = patterns('',
url(r'^$','grapher.views.index', name='index',),
url(r'^plot.png$', 'grapher.views.plot', name='plot'),
)
views.py
def index(request):
states = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID',
'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS',
'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK',
'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV',
'WI', 'WY']
context = RequestContext(request,{'states': states})
return render(request, 'grapher/index.html', context)
def plot(request):
data = scoreData.objects.all()
state = request.GET.get('State','')
postal = request.GET.get('Zip','')
city = request.GET.get('City','')
if state != '':
data = data.filter(state=state)
elif postal != '':
data = data.filter(postal=postal)
elif city != '':
data = data.filter(city=city)
cleanedData = []
for score in data:
cleanedData.append(score.score)
fig = Figure()
ax = fig.add_subplot(1,1,1)
ax.hist(cleanedData,100)
canvas = FigureCanvas(fig)
response = HttpResponse(content_type='image/png')
canvas.print_png(response)
return response
你可以發佈你的django views.py和urls.py代碼嗎? –
使用javascript創建SRC標籤.. –
剛添加到views.py和urls.py的代碼中,謝謝! – ohsully