0

我有这样的行或单词:

hello
ok 
there
hi

我想让用户选择每一行,将该行存储在变量中并使用 DJango 视图进行处理。我怎样才能做到这一点?谢谢

4

2 回答 2

1

假设这些行在网页上,使用一些 jQuery 来处理单击/选择行并将其推送到 django 视图。

比如这种东西(很粗略的伪代码):

HTML:

<table>
<tr>hello</tr>
<tr>ok</tr>
</table>

jQuery:

$(document).ready( {
    $('table tr').onClick(function(){
        $(this).style('color','green'); // to show that its selected
        $.ajax({  type: 'POST',  url: 'django/url',  data: JSON_stringify($(this).text()),   dataType: dataType});
    });
});
于 2012-12-06T11:19:30.043 回答
1

不完全确定您希望用户如何选择每一行,但如果您想使用复选框,这是一个简单的示例,让用户从索引页面中定义的列表中进行选择并在投票视图中按照您的需要进行处理(存储在选择列表变量中):

在你的views.py中:

def index(request):
    mylist = ["hello", "ok", "there", "hi"]
    return render_to_response('testing/index.html', {'mylist': mylist}, context_instance=RequestContext(request))

def vote(request):
    choices = request.POST.getlist('choice')
    return render_to_response('testing/vote.html', {'choices': choices})

在 index.html 中:

<form action="/vote/" method="post">
{% csrf_token %}
{% for choice in mylist %}
    <input type="checkbox" name="choice" id="choice{{ forloop.counter }}" value="{{ choice }}" />
    <label for="choice{{ forloop.counter }}">{{ choice}}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

例如在 vote.html 中:

<html>
<table border="1">
{% for x in choices %}
<tr><td>{{ x }}</td></tr>
{% endfor %}
</table>
</html>

(来自https://docs.djangoproject.com/en/1.4/intro/tutorial04/的修改后的盗版:)

于 2012-12-06T14:35:06.220 回答