私はこのような行または単語を持っています:
hello
ok
there
hi
ユーザーが各行を選択し、その行を変数に格納し、DJangoビューを使用して処理できるようにします。どうやってやるの?ありがとう
私はこのような行または単語を持っています:
hello
ok
there
hi
ユーザーが各行を選択し、その行を変数に格納し、DJangoビューを使用して処理できるようにします。どうやってやるの?ありがとう
これらの行が Web ページ上にあると仮定すると、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});
});
});
ユーザーが各行を選択する方法が正確にはわかりませんが、チェックボックスを使用する場合の簡単な例を次に示します。これにより、ユーザーはインデックスページで定義されたリストから選択し、投票ビューで必要に応じて処理できます(選択肢リスト変数に格納されます):
あなたの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/からの修正されたぼったくり:)