简体   繁体   English

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

[英]Passing variable from template to views function in django

I want to pass a few variables from a template (dropdown form) to a views function. 我想将一些变量从模板(下拉表单)传递给视图函数。 The views functions is not the same, its a different views function. 视图功能不一样,其视图功能不同。

Following is views.py 以下是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')

Following are the templates. 以下是模板。

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>

Second template. 第二个模板。

table.html 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 %}

Following is my urls.py 以下是我的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'),
    )

I'll load the redis.html first at my localhost which will display two dropdown boxes and a submit button. 我将首先在本地主机上加载redis.html,它将显示两个下拉框和一个提交按钮。 I want to send the values selected at the dropdown boxes to be sent to views.table() function. 我想将在下拉框中选择的值发送到views.table()函数。 I know one thing that I have to pass the values via URL only but I am not able to do it properly. 我知道一件事,我只需要通过URL传递值,但是我不能正确地做到这一点。

These values are passed back to the view in a GET request. 这些值在GET请求中传递回视图。 You can access them in your views.table function by: 您可以通过以下方式在views.table函数中访问它们:

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

Keep in mind that the value attribute in the option tags in your template should be unique so that you can differentiate these variables in the GET request. 请记住,模板中选项标签中的value属性应该是唯一的,以便您可以区分GET请求中的这些变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM