在django中将变量从模板传递到视图函数

时间:2014-06-05 11:54:47

标签: python html django

我想将一些变量从模板(下拉表单)传递给视图函数。视图函数不一样,它的视图功能不同。

以下是views.py

from django.shortcuts import get_object_or_404, render
from django.http import Http404, HttpResponse, HttpResponseRedirect
from polls.models import Choice, Poll
from django.core.urlresolvers import reverse
from django.template import RequestContext, loader
from django.views import generic
from django.core.files import File
import os

#a = []
#c = []
def table(request,host,port):
#       host = request.GET['servers']
#       port = request.GET['instances']
#       os.system("redis-cli -h %(host)s -p %(port)s info > /home/ravi/python/info_file/%(host)s_%(port)s.txt" % locals())
        os.system("redis-cli -h %(host)s -p %(port)s info > /home/ravi/python/info_file/%(host)s_%(port)s.txt" % locals())
        with open('/home/ravi/python/info_file/%(host)s_%(port)s.txt' % locals()) as f:
                a = []
                c = []
                for line in f:
                        if not line.startswith('#'):
#                               line = line.strip()
                                if line.strip():
                                        b = line.split(':', 1)
                                        a.append(b[0])
                                        c.append(b[1])
                context = { 'key': a, 'value': c }
        return render(request, 'polls/table.html', context)
        f.close()

def redis(request):
        print "I am here"
        #print request.GET['servers']
        return render(request, 'polls/redis.html')

以下是模板。

redis.html

<form action="{% url polls:table %} " method="get">
        <select name="servers">
                <option value="" disabled="disabled" selected="selected">Please select the server</option>
                <option value="x.x.x.x">server_name</option>
                <option value="x.x.x.x">server_name</option>
        </select>

        <select name="instances">
                <option value="" disabled="disabled" selected="selected">Please select the redis instance</option>
                <option value="port">redis_instance</option>
                <option value="port">redis_instance</option>
        </select>
        <input type="submit" value="Submit">

</form>

第二个模板。

table.html

{% load multifor %}
{% if key %}
    <table border="1" style="width:300px">
    {% for x in key; y in value %}
        <tr>
           <!-- <td>{{ x }}</td> -->
            <td>{{ x }}</td>
            <td>{{ y }}</td>
        </tr>
    {% endfor %}
    </table>
{% else %}
    <p>No info available for this instance.</p>
{% endif %}

以下是我的urls.py

from django.conf.urls import patterns, url

    from polls import views

    urlpatterns = patterns('',

        url(r'^$', views.redis, name='redis'),
        url(r'^index/$', views.table, name='table'),
    )

我首先在我的localhost上加载redis.html,它会显示两个下拉框和一个提交按钮。我想发送在下拉框中选择的值发送到views.table()函数。我知道有一件事我只能通过URL传递值,但我无法正确执行。

1 个答案:

答案 0 :(得分:1)

这些值将传递回GET请求中的视图。您可以通过以下方式在views.table函数中访问它们:

if request.GET:
    values = request.GET
    print values

请注意,模板中选项标记中的value属性应该是唯一的,以便您可以在GET请求中区分这些变量。