我正在尝试将多个复选框的值从html模板传递到views.py,然后在响应中显示这些值。我在我的应用程序中创建了一个forms.py并定义了一个类。然后我将值传递给views.py的pack方法。
模板:
<form method="POST" action="{% url 'pack' %}">
<div id="row-1">
<button name="pack" id="pack">Pack</button>
</div>
<div id="row-2">
<table>
<thead>
<tr>
<th><input type="checkbox" onClick="toggle(this, 'no')"></th>
<th>Order Id</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="no" value="10000001">10000001</td>
</tr>
<tr>
<td><input type="checkbox" name="no" value="10000002">10000002</td>
</tr>
</tbody>
</table>
</div>
</form>
<script type="text/javascript">
function toggle(source, text) {
checkboxes = document.getElementsByName(text);
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
我的views.py:
from django.shortcuts import render
from django.http import HttpResponse
from test1 import forms
def index(request):
return render(request, 'index.html')
def pack(request):
oid_list = []
form = PackOrders(request.POST or None)
if request.method == 'POST':
for item in form.cleaned_data['no']:
oid_list.append(item)
return HttpResponse(oid_list)
forms.py:
from django import forms
class PackOrders(forms.Form):
order_items = forms.CharField()
urls.py:
from django.conf.urls import patterns
from django.conf.urls import url
from test1 import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^index.html', views.index, name='index1'),
url(r'^test/', 'views.pack', name='pack'),)
代码有什么问题?
编辑:投掷Error during template rendering
追溯指向<form method="POST" action="{% url 'pack' %}">
Traceback:
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/manish/syserp/chkbx/test1/views.py" in index
7. return render(request, 'index.html')
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
99. return template.render(context, request)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/template/backends/django.py" in render
74. return self.template.render(context)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/template/base.py" in render
209. return self._render(context)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
493. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
579. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
433. self._populate()
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _populate
308. for name in pattern.reverse_dict:
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse_dict
338. self._populate()
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _populate
326. lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in callback
247. self._callback = get_callable(self._callback_str)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/utils/lru_cache.py" in wrapper
101. result = user_function(*args, **kwds)
File "/home/manish/syserp/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in get_callable
106. mod = import_module(mod_name)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
Exception Type: ImportError at /
Exception Value: No module named views
请指出错误,因为这是我的第一个django项目。
答案 0 :(得分:2)
在表单的html代码中,只需将action属性设置为action ='/ test /'。
再次在views.py
中if request.method == 'POST':
list_of_ids = request.POST.getlist('no')
for id in list_of_ids:
oid_list.append(id)
试试这个,它应该有帮助