2013-05-31 52 views
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 
+0

你可以發佈你的django views.py和urls.py代碼嗎? –

+0

使用javascript創建SRC標籤.. –

+0

剛添加到views.py和urls.py的代碼中,謝謝! – ohsully

回答

0

這是僞代碼,但類似下面應該讓你關閉。我從未用過matplotlib(?),因此base64圖像生成可能關閉。

# models.py 
from django.db import models 
from django.localflavor.us.models import USStateField, USPostalCodeField 


class ScoreData(models.Model): 
    state = USStateField(blank=True) 
    postal = USPostalCodeField(blank=True) 
    city = models.CharField(blank=False, max_length=200) 
    score = models.IntegerField() 

# forms.py 
from cStringIO import StringIO 
import base64 

from django import forms 

from .models import ScoreData 


class PlotForm(forms.ModelForm): 
    class Meta: 
     model = ScoreData 
     fields = ('state', 'postal', 'city') 

    def as_png(self): 
     data = ScoreData.objects.filter(
      **self.cleaned_data 
     ).values_list('score', flat=True) 

     fig = Figure() 
     ax = fig.add_subplot(1, 1, 1) 
     ax.hist(data, 100) 

     io = StringIO() 
     fig.savefig(io, format='png') 

     return base64.encodestring(io.getvalue()) 

# views.py 
from django.shortcuts import render 


def index(request, template_name='plot.html'): 
    context = {} 

    if request.method == 'POST': 
     form = PlotForm(request.POST) 

     if form.is_valid(): 
      context['base64_png'] = form.as_png() 
    else: 
     form = PlotForm() 

    context['form'] = form 

    return render(request, template_name, context) 


# template 
<form action="{% url "index" %}" method="POST"> 
    {% csrf_token %} 
    {{ form.as_p }} 
    <input type="submit" value="Submit" /> 
</form> 

{% if base64_png %} 
<img src="data:image/png;base64,{{ base64_png }}"> 
{% endif %} 
相關問題