将Django表单数据发送到模板中的URL

时间:2013-05-31 00:03:23

标签: django django-forms django-templates

我正在尝试让应用程序让用户访问某个站点,提交带有位置数据的表单以缩小数据库条目,然后根据该信息显示一个图表。用户只需访问索引页面,单击提交按钮即可显示表单下方的图表。在内部,模板调用绘图视图,该视图使用来自请求的信息来过滤数据并返回.png。在我的索引模板中,我有:

<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

1 个答案:

答案 0 :(得分: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 %}