Reputation: 4656
I have a template with checkbox and a button. I want , when i select all checkbox and press the button it should return the values and display it as a httpresponse.
Index.html:
<form method="POST" action="/test/">
{% csrf_token %}
<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>
views.py:
from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from test1 import forms
def index(request):
return render(request, 'index.html')
def pack(request):
oid_list = []
form = forms.PackOrders(request.POST or None)
if request.method == 'POST':
for item in form.cleaned_data['no']:
oid_list.append(item)
return HttpResponse(','.join(oid_list))
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'^test/', views.pack, name='pack'),)
forms.py:
from django import forms
class PackOrders(forms.Form):
order_items = forms.CharField()
The problem is that when i cleck the checkbox and i return the code it just reload the index.html instead of displaying the values of checkbox.
Expected Output:
10000001,10000002
What is wrong with the code?
EDIT :
projects urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('test1.urls')),
url(r'^test/', include('test1.urls')),
]
Upvotes: 0
Views: 65
Reputation: 599590
You've included your app urlconf into your root urlconf twice. You should only do it once. However, you should remove the $ from the URL that includes it:
url(r'^', include('test1.urls')),
If you used the second version instead, with the 'test/' prefix, you would create URLs at '/test/' and '/test/test/', which presumably isn't what you want.
Note though that as I said in the comment, you should always reference URLs via their name, not directly. So your form action should be "{% url 'pack' %}"
.
Upvotes: 2
Reputation: 43300
$
in regex indicates the end of the string so your include will never get chance to match any of its urls (which I imagine is what your other urls.py is). Therefore you need to change your urls to the included urls
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^/', include('test1.urls')),
url(r'^test/', include('test1.urls')),
]
Notice I have changed the second one to remove the $
and replace it with a slash
Upvotes: 0