简体   繁体   中英

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

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

<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

{% 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

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. I want to send the values selected at the dropdown boxes to be sent to views.table() function. I know one thing that I have to pass the values via URL only but I am not able to do it properly.

These values are passed back to the view in a GET request. You can access them in your views.table function by:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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