簡體   English   中英

在Django中將變量從模板傳遞到視圖函數

[英]Passing variable from template to views function in 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'),
    )

我將首先在本地主機上加載redis.html,它將顯示兩個下拉框和一個提交按鈕。 我想將在下拉框中選擇的值發送到views.table()函數。 我知道一件事,我只需要通過URL傳遞值,但是我不能正確地做到這一點。

這些值在GET請求中傳遞回視圖。 您可以通過以下方式在views.table函數中訪問它們:

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

請記住,模板中選項標簽中的value屬性應該是唯一的,以便您可以區分GET請求中的這些變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM